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í),可以省略不寫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)文章
Vue與Node.js通過(guò)socket.io通信的示例代碼
這篇文章主要介紹了Vue與Node.js通過(guò)socket.io通信的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
vue不通過(guò)路由直接獲取url中參數(shù)的方法示例
通過(guò)url傳遞參數(shù)是我們?cè)陂_發(fā)中經(jīng)常用到的一種傳參方法,但通過(guò)url傳遞后改如果獲取呢?下面這篇文章主要給大家介紹了關(guān)于vue如何不通過(guò)路由直接獲取url中參數(shù)的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08
在Vue前端(Vue2/Vue3?通用)載入JSON格式的動(dòng)圖實(shí)例代碼
這篇文章主要介紹了在Vue前端(Vue2/Vue3?通用)載入JSON格式動(dòng)圖的相關(guān)資料,文中通過(guò)代碼講解了環(huán)境配置、組件集成、動(dòng)畫控制等,重點(diǎn)介紹了Lottie的loadAnimation方法的參數(shù)配置和常見問(wèn)題解決方法,需要的朋友可以參考下2025-11-11
VUE中攔截請(qǐng)求并無(wú)感知刷新token方式
這篇文章主要介紹了VUE中攔截請(qǐng)求并無(wú)感知刷新token方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Vue3整合WangEditor富文本編輯器的實(shí)踐指南
這篇文章主要為大家詳細(xì)介紹了如何在?Vue?3?項(xiàng)目中集成?WangEditor?富文本編輯器,實(shí)現(xiàn)圖文混排、自定義擴(kuò)展等高階功能,感興趣的小伙伴可以了解下2025-03-03
在Vue3項(xiàng)目中優(yōu)化使用ECharts的完整方案
在Vue3項(xiàng)目中使用 ECharts,如何做到按需打包、類型安全、響應(yīng)式適配、自動(dòng)銷毀?本文分享一套可直接落地的封裝方案,需要的朋友可以參考下2026-05-05
vue項(xiàng)目中input輸入框輸入不了值問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目中input輸入框輸入不了值問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析
這篇文章主要介紹了基于vue.js仿淘寶收貨地址并設(shè)置默認(rèn)地址的案例分析,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

