Rumah > Soal Jawab > teks badan
P粉7544734682023-08-29 12:38:14
addRoute
fungsi/
(tanpanya anda akan mendapat ralat "Laluan tidak bersarang mesti mengandungi aksara slash terkemuka") $router.push
untuk melompat ke laluan baharuconst Foo = { template: '<div>Foo</div>' } const Home = { template: '<div>Home</div>' } const router = new VueRouter({ mode: 'history', routes: [{ path: '/', component: Home }] }) const app = new Vue({ router, el: "#vue-app", methods: { viewComponent: function(path, method) { let tf = `/${path}/${method}`; let newRoute = { path: tf, name: `${path}_${method}`, component: Foo, } this.$router.addRoute(newRoute) this.$router.push({ name: newRoute.name }) }, } });
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script> <script src="https://npmcdn.com/vue-router/dist/vue-router.js"></script> <div id="vue-app"> <a v-on:click="viewComponent('api/contact','get')">ddd</a> <router-view></router-view> </div>