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 badges
Add a ment  | 

3 Answers 3

Reset to default 2

in 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