「TypeError:this。$ route.Push is not a function」というエラーが表示されますが、ES6でもこれをバインドしようとしましたが、まだ機能しません。 about.vueの「Get Homepage Data」であるボタンをクリックするたびに、ホームページにリダイレクトされます。動作していますが、ここでは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>
ルートを変更するには
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' })
問題は、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("/")
を使用します