我正在尝试根据示例使用vue-router ,例如
let routes = [
{ path: '/', component: MainComponent },
];
let router = new VueRouter({routes});
new Vue({ router }).$mount('#app');
但我总是得到这个错误:
vue.runtime.esm.js:619 [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available.
我可以使用渲染功能解决此问题吗?我试过了,
let routes = [];
但是仍然失败。
好的,我花了半天时间,终于使它起作用:vue-router + webpack +仅运行时的vue。
本教程是最有用的。我学到的是:
如果使用vue-cli,则vue版本位于webpack.base.conf.js中
如果要使用运行时,则必须更改main.js
。千万不要使用此
new Vue({
el: '#app',
router,
template: '<App/>', // <== This is bad
components: { App }
});
而是DO使用
Vue.use(VueRouter); // <== very important
new Vue({
router,
render(createElement) {
return createElement(App);
}
}).$mount('#app');
您可以使用with$mount
或el:
with with运行时。两者都可以工作,但是可以$mount
为您提供更大的灵活性。当然,router
是按照通常的方式创建的
let routes = [
{ path: '/', component: MainComponent },
];
let router = new VueRouter({ routes });
如果您仍然看到错误
[Vue warn]: You are using the runtime-only build of Vue
在控制台中,然后再次确保您从未在代码中使用带有字符串的任何模板。即使您在* .vue文件中,如果尝试这样做
const Foo = { template: '<div>foo</div>' };
它会失败。相反,您必须使用<template>
标签,否则必须使用createElement
。祝好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句