【发布时间】:2018-07-05 07:09:10
【问题描述】:
这是我的组件:
const Vue = require("vue/dist/vue.js");
const qs = require("querystring");
module.exports = Vue.component("Page",function(resolve){
console.log($route);
let id = this.$route.params.id;
fetch("/getPage.json",{
method:"POST",
body:qs.stringify({
id
})
}).then(r=>r.json())
.then(j=>{
console.log(j);
resolve({
template:`<h1>`+JSON.stringify(j)+"</h1>"
});
})
});
这是我的路由器:
const router = new VueRouter({
mode:"history",
routes:[
{
path:"/",
component:Home
},
{
path:"/me",
component:Me
},
{
path:"/create-page",
component:createPage
},
{
path:"/p/:id",
component:Page
}
]
});
Vue.use(VueRouter);
当我运行这个应用程序时,我得到:
ReferenceError: $route is not defined
我尝试使用this.$route,但它不起作用。我正在使用箭头功能。当我这样做时,它会起作用:
const Vue = require("vue/dist/vue.js");
module.exports = Vue.component("Page",function(resolve){
resolve({
template:`<h1>{{$route.params.id}}`
});
});
请帮我弄清楚如何解决这个问题。
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component vue-router