我遇到了类似“ TypeError:this。$ route.push不是函数”的错误,即使我尝试将ES6也绑定到它,仍然无法正常工作。每当我单击about.vue中的“获取主页数据”按钮时,它都应重定向到主页。正在工作,但在这里我需要调用ajax,并且在ajax响应之后,它只必须重定向,以便我不使用
router.js
import Vue from "vue";
import Router from "vue-router";
import Home from "../components/Home/Home.vue";
import About from "../components/About.vue";
import FallBackPage from "../components/FallBackPage/FallBackPage.vue";
import MyOrders from "../components/MyOrders/MyOrders.vue";
Vue.use(Router);
export function createRouter() {
return new Router({
mode: "history",
routes: [
{ path: "/", component: Home },
{ path: "/about", component: About },
{ path: "/myorders", component: MyOrders },
{ path: "/*", component: FallBackPage }
]
});
}
main.js
import Vue from "vue";
import App from "./App.vue";
import { createRouter } from "./router/router.js";
export function createApp() {
const router = createRouter();
const app = new Vue({
router,
render: h => h(App)
});
return { app, router };
}
about.vue
<template>
<div>
{{ aboutText }}
<button @click="gobackToHome">Get Homepage Data</button>
</div>
</template>
<script>
export default {
data() {
return {
aboutText: "About Component"
};
},
method: {
gobackToHome: function() {
this.$route.push("/")
// $route or $router both are not working
}
// gobackToHome: () => {
// this.$route.push("/")
// }
}
};
</script>
答案 0 :(得分:1)
我认为问题是您没有将vue-router导入到您的应用中。仅导入router.js,然后您不会将所有router.js都导入到您的应用中,仅导入createRouter函数。试试这个:
//routes.js
import Home from "../components/Home/Home.vue";
import About from "../components/About.vue";
import FallBackPage from "../components/FallBackPage/FallBackPage.vue";
import MyOrders from "../components/MyOrders/MyOrders.vue";
export const routes [
{ path: "/", component: Home },
{ path: "/about", component: About },
{ path: "/myorders", component: MyOrders },
{ path: "/*", component: FallBackPage }
]
//main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
import {routes} from './routes'
Vue.use(VueRouter)
const router = new VueRouter({
mode: 'history',
routes
})
new Vue({
router,
render: h => h(App)
}).$mount('#app')
然后在组件中使用this.$router.push("/")
答案 1 :(得分:0)
更改路线
this.$router.push({ path: '/' })
this.$router.push({ name: 'Home' })
// main.js
import Vue from "vue";
import App from "./App.vue";
import VueRouter from "vue-router";
import { routes } from "./router/router";
Vue.use(VueRouter);
export function createApp() {
const router = new VueRouter({
mode: "history",
routes
});
const app = new Vue({
router,
render: h => h(App)
}).$mount("#app");
return { app, router };
}
// routes.js
import Home from "../components/Home/Home.vue";
import About from "../components/About.vue";
import FallBackPage from "../components/FallBackPage/FallBackPage.vue";
import MyOrders from "../components/MyOrders/MyOrders.vue";
export const routes = [
{ path: "/", component: Home },
{ path: "/about", component: About },
{ path: "/myorders", component: MyOrders },
{ path: "/*", component: FallBackPage }
]
在main.js更改上更改路由,我们需要使用以下
this.$router.push({ path: '/' })
this.$router.push({ name: 'Home' })