Vue2路由router的安裝和使用完整實(shí)例
上篇文章給大家介紹了關(guān)于Vue3路由push跳轉(zhuǎn)問(wèn)題(解決Vue2this.$router.push失效) 喜歡的朋友點(diǎn)擊查看,今天繼續(xù)給大家介紹關(guān)于Vue2路由router的相關(guān)知識(shí)。正文如下:
一、如何安裝路由
第一步:在終端輸入命令npm i vue-router@3
第二步:出現(xiàn)added 1 package in 2m表示安裝成功

二、vue-router配置環(huán)境
第一步:在src路徑中新建一個(gè)router文件夾,放置index.js

第二步:在index.js文件中導(dǎo)入路由:import VueRouter from 'vue-router'
第三步:在index.js文件中使用路由:Vue.use(VueRouter)

第四步:在main.js文件中引入路由文件:
注意:router文件夾中的index.js文件在導(dǎo)入時(shí),可以省略不寫(xiě)index:import router from'./router'
三、如何使用路由
靜態(tài)路由
1、聲明式
<template>
<div>
<router-link to="/">Home</router-link>
<router-link to="/about">About</router-link>
</div>
</template>2、編程式
// 在組件中
this.$router.push('/about')動(dòng)態(tài)路由
編程式路由導(dǎo)航來(lái)傳遞參數(shù)
在路由配置中定義動(dòng)態(tài)路由參數(shù):
routes: [
{
path: '/uploadFile/:id',
name: 'uploadFile',
component: uploadFile
}
]在組件中使用 $router.push 方法進(jìn)行編程式導(dǎo)航并傳遞參數(shù):
openDialog(row){
this.$router.push({ name: 'uploadFile', params: { id: row.id } });
},這里的 id 就是你想要傳遞的參數(shù)。
在接收參數(shù)的組件中通過(guò) $route.params 獲取傳遞的參數(shù):
mounted() {
const id = this.$route.params.id;
},完整案例
說(shuō)明:通過(guò)另一個(gè)goodsList頁(yè)面中的表格,點(diǎn)擊任何一條即可跳轉(zhuǎn)到uploadFile頁(yè)面
在 goodsList模板中顯示表格
<el-table :data="goodsData" border style="width: 100%">
<el-table-column prop="id" label="id" width="180">
</el-table-column>
<el-table-column prop="name" label="商品名稱" width="180">
</el-table-column>
<el-table-column prop="price" label="商品價(jià)格" width="180">
</el-table-column>
<el-table-column prop="imageUrl" label="商品圖片" width="180">
</el-table-column>
<el-table-column prop="status" label="狀態(tài)">
</el-table-column>
<el-table-column prop="name" label="操作" align="center">
<template slot-scope="scope">
<!-- (scope.row.userId)用于獲取當(dāng)前行數(shù)據(jù)對(duì)象中的用戶ID(或其他字段) -->
<el-button size="mini" type="text" @click="openDialog(scope.row)">編輯
</el-button>
</template>
</el-table-column>
</el-table>在路由配置中定義動(dòng)態(tài)路由參數(shù):
{
path: '/uploadFile/:id',
name: 'uploadFile',
component: uploadFile
}, {
path: '/goodsList',
component: goodsList
}在goodsList組件中使用 $router.push 方法進(jìn)行編程式導(dǎo)航并傳遞參數(shù):
methods: {
openDialog(row) {
this.$router.push({ name: 'uploadFile', params: { id: row.id } });
},
} 這里的 id 就是你想要傳遞的參數(shù)。
在接收參數(shù)的uploadFile組件中通過(guò) $route.params 獲取傳遞的參數(shù):
mounted() {
const id = this.$route.params.id;
this.selectById(id);
},
methods: {
selectById(id){
this.$axios({
method:'post',
url:'http://localhost:8080/api/upload/selectGoods',
data:{
id:id
}
}).then((res)=>{
console.log("4444"+JSON.stringify(res ));
this.fileList.push( {name: res.data.data.list[0].name, url: res.data.data.list[0].imageUrl})
this.name=res.data.data.list[0].name
this.price=res.data.data.list[0].price
})
},
}到此這篇關(guān)于Vue2路由router的安裝和使用完整實(shí)例的文章就介紹到這了,更多相關(guān)Vue2路由router內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3 中的數(shù)據(jù)偵測(cè)的實(shí)現(xiàn)
這篇文章主要介紹了Vue3 中的數(shù)據(jù)偵測(cè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Vue Element 分組+多選+可搜索Select選擇器實(shí)現(xiàn)示例
這篇文章主要介紹了Vue Element 分組+多選+可搜索Select選擇器實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
vue+element下日期組件momentjs轉(zhuǎn)換賦值問(wèn)題解決
這篇文章主要介紹了vue+element下日期組件momentjs轉(zhuǎn)換賦值問(wèn)題,記錄下使用momentjs轉(zhuǎn)換日期字符串賦值給element的日期組件報(bào)錯(cuò)問(wèn)題,需要的朋友可以參考下2024-02-02
vue 音樂(lè)App QQ音樂(lè)搜索列表最新接口跨域設(shè)置方法
這篇文章主要介紹了vue 音樂(lè)App QQ音樂(lè)搜索列表最新接口跨域設(shè)置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
vue使用$store.commit() undefined報(bào)錯(cuò)的解決
這篇文章主要介紹了vue使用$store.commit() undefined報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue+Element?ui實(shí)現(xiàn)照片墻效果
這篇文章主要為大家詳細(xì)介紹了vue+Element?ui實(shí)現(xiàn)照片墻效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
VUE引入騰訊地圖并實(shí)現(xiàn)軌跡動(dòng)畫(huà)的詳細(xì)步驟
這篇文章主要介紹了VUE引入騰訊地圖并實(shí)現(xiàn)軌跡動(dòng)畫(huà),引入步驟大概是在 html 中通過(guò)引入 script 標(biāo)簽加載API服務(wù),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09

