首页 > 解决方案 > canjs中的路由不起作用,因为链接没有获得href值

问题描述

我是 canjs 的新手,并按照 canjs 网站上的教程进行操作。我正在使用 webpack 创建一个示例 canjs 页面。

import { Component, stacheRouteHelpers } from "can";

Component.extend({
    tag: "my-app",
    view: `
        <a href="{{ routeUrl(page='home') }}">Home</a>
        <a href="{{ routeUrl(page='tasks') }}">Tasks</a>
    `,
    ViewModel: {
    }
});

当我在页面中看到时,它没有路由到家。Infact href 值不会像下面那样出现在输出中。

<a href="">Home</a>

控制台显示以下警告

MyAppView:3: Unable to find key "routeUrl()".

标签: routingcanjs

解决方案


就像 Justin 提到的那样,我认为 webpack 是stacheRouteHelpers在构建中摇树的。

我认为您可以通过执行以下操作来解决此问题:

import { Component, stache, stacheRouteHelpers } from "can";

stache.addHelper(stacheRouteHelpers);

推荐阅读