最近在練習vue,使用cdn在index.html引入vue和vue-router,並且使用httpVueLoader套件編譯vue檔案,設定路由時遇到一個問題
目前做到的是網址列會隨著我的sidebar選擇改變但router-view 沒有顯示
不知道是component擺放的方式不對還是路由設定錯誤,再麻煩大神們為我解惑
//上略
<div id="app">
<app-content></app-content>
</div>
<script src="./api/server.js"></script>
<script>
const routes = [
{ path: "/",component: httpVueLoader("./view/app-content.vue")},
{ path: "/dashboard", component: httpVueLoader("./view/dashboard.vue") },
{ path:"/transaction",component:httpVueLoader("./view/transaction.vue") },
{ path: "/payment", component: httpVueLoader("./view/payment.vue") },
{ path: "/setting", component: httpVueLoader("./view/setting.vue") },
];
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch((err) => err);
};
const router = new VueRouter({
mode: "history",
base: "/dist/",
routes,
});
var app = new Vue({
router,
data: {
},
components: {
'app-content': httpVueLoader('./view/app-content.vue'),
},
methods: {
},
}).$mount('#app')
</script>
<template>
<div>
<login @user-login="getLogin()"></login>
<sidebar @uri="uri()"></sidebar>
<div class="content">
<router-view></router-view>
</div>
</div>
</template>
<script>
;
module.exports = {
data: function () {
return {
enter: false,
componentName: "",
};
},
mounted() {},
components: {
login: httpVueLoader("./login.vue"),
sidebar: httpVueLoader("./sidebar.vue"),
},
methods: {
uri(uri) {
this.componentName = uri;
this.$router.push(this.componentName);
},
getLogin(boolean) {
this.enter = boolean;
},
},
};
</script>
<template>
<div>
<nav class="sidebar">
<img src="images/logo.svg" alt="" class="mt-4 mb-4" />
<p class="nav_item">
<img src="images/dashboard-s.svg" class="mr-1" />
<router-link to="/dashboard" @click.native="send('/dashboard')"
>總覽</router-link
>
</p>
<p class="nav_item">
<img src="images/search-s.svg" class="mr-1" />
<router-link to="/transaction" @click.native="send('/transaction')"
>查詢</router-link
>
</p>
<p class="nav_item">
<img src="images/payment-s.svg" class="mr-1" />
<router-link to="/payment" @click.native="send('/payment')"
>列表</router-link
>
</p>
<p class="nav_item">
<img src="images/more-s.svg" class="mr-1" />
<router-link to="/setting" @click.native="send('/setting')"
>設定</router-link
>
</p>
</nav>
</div>
</template>
<script>
module.exports = {
data: function () {
return {};
},
components: {},
methods: {
send(uri) {
this.$emit("uri", uri);
},
},
};
</script>
問題關鍵在 sidebar 接 emit 的地方:
<sidebar @uri="uri()"></sidebar>
改成
<sidebar @uri="uri"></sidebar>
讓 uri(uri) 的 uri 參數可以接到資料。
uri(uri) {
this.componentName = uri;
this.$router.push(this.componentName);
}
感謝回答
顯示問題已解,修改後得到vue的錯誤提示是因為
Non-nested routes must include a leading slash character
記得定義routes的地方,最外層的路由的 Path 最前方要加上/
const routes = [
{ path: "/",component: httpVueLoader("./view/app-content.vue")},
{ path: "/dashboard", component: httpVueLoader("./view/dashboard.vue") },
{ path:"/transaction",component:httpVueLoader("./view/transaction.vue") },
{ path: "/payment", component: httpVueLoader("./view/payment.vue") },
{ path: "/setting", component: httpVueLoader("./view/setting.vue") },
];
恩恩感謝~
您好,在一樣的程式碼我在切換頁面時得到這個錯誤,但是我對這個錯誤沒有頭緒,還是只是單純httpVueLoader 這個套件的問題,有任何可能原因再請指導,感謝
想問一下為什麼你的網址,會在 http://127.0.0.1:5501/dist 底下,而不是在 http://127.0.0.1:5501/ 底下?
從錯誤訊息看起來,是找不到你的各路由 .vue 檔案。
我好像知道嘞,因為我把它放到IIS上然後設定在dist資料夾內,但是我自己本地端測試時沒有修改回來,感謝感謝~
不顯示是一片空白嗎?
可能是哪個地方寫錯了 XD