本篇文章給大家分享的是有關(guān)怎么在vue中使用 $router.push實現(xiàn)兩組件間值傳遞,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
成都創(chuàng)新互聯(lián)專注于南皮企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城建設(shè)。南皮網(wǎng)站建設(shè)公司,為南皮等地區(qū)提供建站服務(wù)。全流程按需制作,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)vue是什么Vue是一套用于構(gòu)建用戶界面的漸進(jìn)式JavaScript框架,Vue與其它大型框架的區(qū)別是,使用Vue可以自底向上逐層應(yīng)用,其核心庫只關(guān)注視圖層,方便與第三方庫和項目整合,且使用Vue可以采用單文件組件和Vue生態(tài)系統(tǒng)支持的庫開發(fā)復(fù)雜的單頁應(yīng)用。
vue文件部分:
<tree :data="dataList" node-key="id" default-expand-all :expand-on-click-node="false"> <span class="custom-tree-node" slot-scope="{ node, data }" :class="$style.list"> <span :class="$style.listText">{{ node.label }}</span> <span :class="$style.listBtn"> <button :class="$style.btn" type="text" size="mini" @click="() => edit(data)"> </button> </span> </span> </tree> <router-view></router-view>
這是封裝好的樹狀列表,使用 scoped slot 會傳入兩個參數(shù) node 和 data ,分別表示當(dāng)前節(jié)點(diǎn)的 Node 對象和當(dāng)前節(jié)點(diǎn)的數(shù)據(jù)。當(dāng)點(diǎn)擊button會路由跳轉(zhuǎn)頁面顯示在 <router-view>中。
那我們先來看一下ts中edit這個方法是怎么寫的。
edit(info: Idata) { this.$router.push({ name: `ListEdit`, query: { label: info.label, scene: info.scene, }, }); },
終于看到主角 $router.push ,它會帶兩個參數(shù),name表示即將跳轉(zhuǎn)到的路由名字,還有一個參數(shù)可以是query,也可以是params,它們的區(qū)別簡單來說,就相當(dāng)于 get 和 post ,query == get ,params == post,query 會把攜帶的參數(shù)顯示在 url 中。那query中的參數(shù)就是所需要攜帶的參數(shù),那這一步總體來說就意味著跳轉(zhuǎn)到ListEdit這個路由頁面,并攜帶label、scene 這兩個參數(shù)。
至于其中的 info:Idata 這樣的寫法是因為ts,ts接口了解一下。
現(xiàn)在編輯按鈕這部分內(nèi)容ok了,它確定了要跳轉(zhuǎn)的地方還有需要攜帶的參數(shù),那么我們這個ListEdit路由頁面就應(yīng)該做好準(zhǔn)備接收人家?guī)淼膮?shù)呀。在頁面創(chuàng)建期間和路由發(fā)生改變期間,都會有一個傳值的動作,那就再created鉤子函數(shù)和監(jiān)聽路由函數(shù)中寫入代碼。
created() { const {label= "", scene= ""} = this.$route.query; this.form = { name: label.toString(), initScene: scene.toString(), }; }, watch: { $route(to, from) { if (to.path === "/list/listEdit") { const {label= "", scene= ""} = to.query; this.form = { name: label.toString(), initScene: scene.toString(), }; } }, },
我感覺這樣半截的代碼實在難以說明,name、initScene都是前面定義的,還是放出完整代碼體驗一下吧。
樹狀列表編輯按鈕vue文件部分:
<template> <tree :data="dataList" node-key="id" default-expand-all :expand-on-click-node="false"> <span class="custom-tree-node" slot-scope="{ node, data }" :class="$style.list"> <span :class="$style.listText">{{ node.label }}</span> <span :class="$style.listBtn"> <button :class="$style.btn" type="text" size="mini" @click="() => edit(data)"> </button> </span> </span> </tree> <router-view></router-view> </template> <script src="./index.ts" lang="ts"></script>
樹狀列表編輯按鈕ts文件部分:
import Vue from "vue"; interface Idata { id: string; label: string; scene: string; children?: Idata[]; } export default Vue.extend({ data() { const data: Idata[] = [{ id: "1", label: "1", scene: "場景1", }, { id: "2", label: "2", scene: "場景2", children: [{ id: "4", label: "2-1", scene: "場景1", }], }, { id: "3", label: "3", scene: "場景2", }]; return { data, dataList: JSON.parse(JSON.stringify(data)), }; }, methods: { edit(info: Idata) { this.$router.push({ name: `VisListEdit`, query: { label: info.label, scene: info.scene, }, }); }, }, });
這里,ts接口定義可以遞歸實現(xiàn),children的類型定義還是Idata,就可以直接自我調(diào)用。
ListEdit 路由頁面vue文件部分:
<template> <div> <form :model="form" ref="form"> <form-item :label="目錄名稱"> <input v-model="form.name"></input> </form-item> <form-item :label="選擇場景"> <select v-model="form.initScene" placeholder="請輸入場景"> <option v-for="item in sceneOption" :key="item.id" :label="item.name" :value="item.id"> </option> </select> </form-item> </form> <div> <button type="primary" @click="submitForm">保存</button> </div> </div> </template> <script src="./index.ts" lang="ts"></script>
ListEdit 路由頁面ts文件部分:
import Vue from "vue"; interface Iscenes { id: string; name: string; selected: boolean; } export default Vue.extend({ data() { const sceneOption: Iscenes[] = [{ id: "1", name: "場景1", selected: false, },{ id: "2", name: "場景2", selected: false, },{ id: "3", name: "場景3", selected: false, }]; return { form: { name: "", initScene: "", }, sceneOption, }; }, created() { const {label= "", scene= ""} = this.$route.query; this.form = { name: label.toString(), initScene: scene.toString(), }; }, watch: { $route(to, from) { if (to.path === "/list/listEdit") { const {label= "", scene= ""} = to.query; this.form = { name: label.toString(), initScene: scene.toString(), }; } }, }, methods: { submitForm() { console.log("test"); } }, });
最后,再來看一下,路由部分的配置:
import ListDetail from "../views/list-detail/index.vue"; import List from "../views/list/index.vue"; import { MenuConfig } from "./index"; export const listRouter: MenuConfig = { path: "/list", component: List, title: "目錄管理", key: "list", name: "list", hasPermission: true, subShow: false, children: [{ path: "listEdit", title: "編輯目錄", hasPermission: true, name: "ListEdit", key: "ListEdit", component: ListDetail, }], };
以上就是怎么在vue中使用 $router.push實現(xiàn)兩組件間值傳遞,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在vue中使用$router.push實現(xiàn)兩組件間值傳遞-創(chuàng)新互聯(lián)
本文鏈接:http://muchs.cn/article48/dhishp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、網(wǎng)站策劃、自適應(yīng)網(wǎng)站、商城網(wǎng)站、靜態(tài)網(wǎng)站、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容