vue-router解決相同路徑跳轉(zhuǎn)報(bào)錯(cuò)的問(wèn)題
vue-router解決相同路徑跳轉(zhuǎn)報(bào)錯(cuò)
剛寫(xiě)完一個(gè)vue的項(xiàng)目,現(xiàn)在總結(jié)和記錄下項(xiàng)目中遇到的問(wèn)題,加強(qiáng)自己,并且分享給你們。
昨天也看了一下項(xiàng)目,忘記記錄,今天還在看項(xiàng)目,就記錄下。
今天看到路由的時(shí)候,看到下面這句代碼,不知道什么作用了
VueRouter.prototype.push = function push(location) {
? ? return routerPush.call(this, location).catch(error=> error)
}注釋之后才想起來(lái),進(jìn)相同的路徑會(huì)報(bào)錯(cuò),加上這句代碼后,就會(huì)不會(huì)報(bào)錯(cuò)了。順便說(shuō)一嘴,就是進(jìn)入相同的路徑不會(huì)刷新數(shù)據(jù),我用的方法是監(jiān)聽(tīng)我們項(xiàng)目的環(huán)節(jié)num,只要這個(gè)改變,就重選請(qǐng)求數(shù)據(jù)。
大致意思就是監(jiān)聽(tīng)一個(gè)會(huì)改變的變量,重新請(qǐng)求數(shù)據(jù)才會(huì)刷新數(shù)據(jù)。
這個(gè)問(wèn)題,當(dāng)時(shí)做項(xiàng)目的時(shí)候貌似查了很久,希望可以幫助到你。
vue常見(jiàn)錯(cuò)誤解決
1.運(yùn)行vue時(shí)瀏覽器報(bào)錯(cuò)Unknown custom element: <custom-select> - did you register the component correctly? For recursive components, make sure to provide the "name" option
原因:被引用的組件頁(yè)面沒(méi)有進(jìn)行export,導(dǎo)致尋找不到瀏覽器console報(bào)錯(cuò),但是編譯的時(shí)候沒(méi)有語(yǔ)法問(wèn)題不報(bào)錯(cuò)
解決:
方法1: export { default as AppMain } from './AppMain'
方法2:將vue/dist/vue.esm.js注銷,修改為vue/dist/vue.min.js

2.vue router 報(bào)錯(cuò)Uncaught (in promise) NavigationDuplicated {_name:""NavigationDuplicated"... 的解決方法
router-link 會(huì)造成報(bào)錯(cuò)的問(wèn)題, 報(bào)錯(cuò)內(nèi)容為:

(1)解決方法很簡(jiǎn)單,把項(xiàng)目依賴的 node_modules 文件夾刪除, 然后再 npm install 重新下載依賴包就可以解決
(2)發(fā)現(xiàn)以上方法很多人都不能成功解決,經(jīng)過(guò)多次嘗試發(fā)現(xiàn)原因可能是 在重新下載依賴包時(shí),安裝的vue-router還是之前出錯(cuò)的那個(gè)版本,那么要怎么解決呢?解決方法也很簡(jiǎn)單,在項(xiàng)目目錄下運(yùn)行 npm i vue-router@3.0 -S 即可
(3)在main.js下添加一下代碼:
import Router from 'vue-router'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
? return originalPush.call(this, location).catch(err => err)
}3.Vue報(bào)錯(cuò) [Vue warn]: Property or method "name" is not defined on the instance but referenced.....
原因:在data中沒(méi)有定義一個(gè)name, 致錯(cuò)
解決方法:在data中定義一個(gè)name=" ",
[Vue warn]: Property or method "value" is not defined on the instance but referenced.....
原因:template中定義了屬性,如v-model,但在data中沒(méi)有定義一個(gè)value
解決方法:在data中定義一個(gè)value=" ",
4.Error in render: "TypeError: Cannot read property ‘list’ of undefined"
**報(bào)錯(cuò):**渲染錯(cuò)誤:“未定義的Type Error:無(wú)法讀取屬性”列表
**原因:**沒(méi)給list定義,也就是說(shuō)在temple中用到list了,但是在data中沒(méi)定義這個(gè)字段,如果已經(jīng)定義了但是還是報(bào)錯(cuò),請(qǐng)檢查下自己是否拼錯(cuò)了單詞,因?yàn)槲揖褪沁@么蠢了= =
解決:
data () {
? return {
? ? list: []
? }
},5.[Vue warn]: Property or method “message” is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property
報(bào)錯(cuò):message沒(méi)定義
原因:跟上面的一樣,message在data那里沒(méi)有定義,定義一個(gè)初始值就好
解決:
data() {
?return {
? ? ?message: ''
? }
},6.Module build failed: Error: No parser and no file path given, couldn’t infer a parser.
報(bào)錯(cuò):沒(méi)有語(yǔ)法分析器和文件路徑,無(wú)法推斷解析器
原因:依賴包出現(xiàn)問(wèn)題,prettier 一個(gè)vue-cli的依賴,把一個(gè)feature 的移除當(dāng)作次版本發(fā)布
解決:npm install --save-dev prettier@1.12.0(刪除 node_modules下_prettier@1.13.0@prettier文件夾)
7.routes forEach is not a function
原因:forEach routes沒(méi)有發(fā)現(xiàn)里面有值
解決:
1.查看import {routes} from './routes’這個(gè)路徑是否正確
2.routes是一個(gè)數(shù)組,檢查routes是否是一個(gè)數(shù)組
3.是否已經(jīng)new了一個(gè)router,又再次new一遍?
// main.js
// 路由配置
const RouterConfig = {
? // 使用HTML5的History模式
? mode: 'history',
? routes: Routers
}
// new VueRouter
const router = new VueRouter(RouterConfig)
?
?
// router.js
// 在router中又再次new一遍,重復(fù)了?。。?!
export default new Router({
? routes: [
? ? {
? ? ? path: '/',
? ? ? name: 'home',
? ? ? component: home
? ? }
? ]
})?改為:
// router.js
const routers = [
? {
? ? path: '/home',
? ? meta: {
? ? ? title: '主頁(yè)'
? ? },
? ? component: (resolve) => require(['../page/home.vue'], resolve)
]
export default routers8.[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the “name” option.
原因:被引用的組件頁(yè)面沒(méi)有進(jìn)行export,導(dǎo)致尋找不到瀏覽器console報(bào)錯(cuò),但是編譯的時(shí)候沒(méi)有語(yǔ)法問(wèn)題不報(bào)錯(cuò)
解決:
?export { default as AppMain } from './AppMain'9.TypeError: Cannot read property ‘vue’ of undefined
報(bào)錯(cuò)信息:ERROR in ./src/login.vue Module build failed (from ./node_modules/_vue-loader@13.7.3@vue-loader/index.js): TypeError: Cannot read property ‘vue’ of undefined at Object.module.exports (F:\VistualStudioCode\threess\node_modules_vue-loader@13.7.3@vue-loader\lib\load er.js:61:18) @ ./src/main.js 7:13-35 @ multi ./node_modules/_webpack-dev-server@3.1.10@webpack-dev-server/client?http://localhost:3000 (webpack)/h ot/dev-server.js ./src/main.js
原因:vue-loader這個(gè)插件被破壞了
解決:
// 重新安裝依賴 npm install vue-loader@latest --save-dev
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue-router實(shí)現(xiàn)簡(jiǎn)單vue多頁(yè)切換、嵌套路由、路由跳轉(zhuǎn)的步驟和報(bào)錯(cuò)
- Vue通過(guò)vue-router實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的全過(guò)程
- 登錄頁(yè)面的實(shí)現(xiàn)及跳轉(zhuǎn)代碼實(shí)例(vue-router)
- vue-router(this.$router)如何在新窗口打開(kāi)路由跳轉(zhuǎn)頁(yè)面
- vue-router使用next()跳轉(zhuǎn)到指定路徑時(shí)會(huì)無(wú)限循環(huán)問(wèn)題
- Vue-router跳轉(zhuǎn)和location.href的區(qū)別及說(shuō)明
相關(guān)文章
vue.js根據(jù)代碼運(yùn)行環(huán)境選擇baseurl的方法
本篇文章主要介紹了vue.js根據(jù)代碼運(yùn)行環(huán)境選擇baseurl的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
vue3中$attrs的變化與inheritAttrs的使用詳解
$attrs現(xiàn)在包括class和style屬性。?也就是說(shuō)在vue3中$listeners不存在了,vue2中$listeners是單獨(dú)存在的,在vue3?$attrs包括class和style屬性,?vue2中?$attrs?不包含class和style屬性,這篇文章主要介紹了vue3中$attrs的變化與inheritAttrs的使用?,需要的朋友可以參考下2022-10-10
Vue+thinkphp5.1+axios實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Vue+thinkphp5.1+axios實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng)
這篇文章主要介紹了Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
vue3+vite中報(bào)錯(cuò)信息處理方法Error: Module “path“ has&nb
這篇文章主要介紹了vue3+vite中報(bào)錯(cuò)信息處理方法Error: Module “path“ has been externalized for browser compatibility...,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Vue項(xiàng)目發(fā)布后瀏覽器緩存問(wèn)題解決方案
在vue項(xiàng)目開(kāi)發(fā)中一直有一個(gè)令人都疼的問(wèn)題,就是緩存問(wèn)題,這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目發(fā)布后瀏覽器緩存問(wèn)題的解決方案,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09

