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

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

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

第四步:在main.js文件中引入路由文件:
注意:router文件夾中的index.js文件在導(dǎo)入時,可以省略不寫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')動態(tài)路由
編程式路由導(dǎo)航來傳遞參數(shù)
在路由配置中定義動態(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ù)的組件中通過 $route.params 獲取傳遞的參數(shù):
mounted() {
const id = this.$route.params.id;
},完整案例
說明:通過另一個goodsList頁面中的表格,點(diǎn)擊任何一條即可跳轉(zhuǎn)到uploadFile頁面
在 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="商品價格" 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ù)對象中的用戶ID(或其他字段) -->
<el-button size="mini" type="text" @click="openDialog(scope.row)">編輯
</el-button>
</template>
</el-table-column>
</el-table>在路由配置中定義動態(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組件中通過 $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的安裝和使用完整實例的文章就介紹到這了,更多相關(guān)Vue2路由router內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue Element 分組+多選+可搜索Select選擇器實現(xiàn)示例
這篇文章主要介紹了Vue Element 分組+多選+可搜索Select選擇器實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
vue+element下日期組件momentjs轉(zhuǎn)換賦值問題解決
這篇文章主要介紹了vue+element下日期組件momentjs轉(zhuǎn)換賦值問題,記錄下使用momentjs轉(zhuǎn)換日期字符串賦值給element的日期組件報錯問題,需要的朋友可以參考下2024-02-02
vue 音樂App QQ音樂搜索列表最新接口跨域設(shè)置方法
這篇文章主要介紹了vue 音樂App QQ音樂搜索列表最新接口跨域設(shè)置方法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09
vue使用$store.commit() undefined報錯的解決
這篇文章主要介紹了vue使用$store.commit() undefined報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
VUE引入騰訊地圖并實現(xiàn)軌跡動畫的詳細(xì)步驟
這篇文章主要介紹了VUE引入騰訊地圖并實現(xiàn)軌跡動畫,引入步驟大概是在 html 中通過引入 script 標(biāo)簽加載API服務(wù),結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09

