admin管理员组文章数量:1404041
This is my ponent:
const Vue = require("vue/dist/vue.js");
const qs = require("querystring");
module.exports = Vueponent("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>"
});
})
});
This is my ponent:
const Vue = require("vue/dist/vue.js");
const qs = require("querystring");
module.exports = Vue.ponent("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>"
});
})
});
And this is my router:
const router = new VueRouter({
mode:"history",
routes:[
{
path:"/",
ponent:Home
},
{
path:"/me",
ponent:Me
},
{
path:"/create-page",
ponent:createPage
},
{
path:"/p/:id",
ponent:Page
}
]
});
Vue.use(VueRouter);
When i run this app i get:
ReferenceError: $route is not defined
I tried using this.$route
but it's not working. I'm using an arrow function. When i do this it works:
const Vue = require("vue/dist/vue.js");
module.exports = Vue.ponent("Page",function(resolve){
resolve({
template:`<h1>{{$route.params.id}}`
});
});
Please help me figure out how to fix this.
Share edited Jan 26, 2018 at 7:33 try dahan asked Jan 25, 2018 at 22:31 try dahantry dahan 1131 silver badge6 bronze badges3 Answers
Reset to default 2in your main vue app
new Vue(...)
you have to use vue-router first
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
...
new Vue(...)
...
and the go ahead with route declaration
you forgot to add router
in Vue
instance
in you main.js or app.js or index.js (entry point)
const app = new Vue({
router
})
documentation
You should NOT use arrow functions
data: function() {
return {
usertype: this.$route.params.type
};
},
This worked for me.
本文标签: javascriptVueJS Router route is not definedStack Overflow
版权声明:本文标题:javascript - VueJS Router: $route is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744651245a2617697.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论