上節我用element-ui
與 vue-router
實現了頁面跳轉的功能。vue
回想一下,vue-router
實現組件之間的切換關鍵也就倆東西,一個router-link
一個router-view
,因此說整體來講上一節也沒學啥。vue-router
props
父組件向子組件傳值element-ui
$emit
子組件向父組件傳值ui
基於上一節的例子,腦子裏構思了一個點擊table
的row
,彈出框顯示本行信息這樣一個畫面this
要點:spa
props: ['student']
告訴父組件我(彈出框組件)須要一個student
this.$emit('confirm', this.form)
觸發父組件中,彈出框組件上定義的confirm
事件,並將this.form
傳遞過去student-list-info
組件完整代碼:3d
<template> <div> <el-button icon="el-icon-more" @click="dialogFormVisible = true" circle></el-button> <el-dialog title="查詢" :visible.sync="dialogFormVisible"> <el-form :model="student"> <el-form-item label="姓名"> <el-input v-model="student.name"></el-input> </el-form-item> <el-form-item label="性別"> <el-input v-model="student.sex"></el-input> </el-form-item> <el-form-item label="年齡"> <el-input v-model.number="student.age" type="number"></el-input> </el-form-item> </el-form> <div slot="footer" class="dialog-footer"> <el-button @click="dialogFormVisible = false">取 消</el-button> <el-button type="primary" @click="doConfirm(student)">確 定</el-button> </div> </el-dialog> </div> </template> <script> export default { name: 'student-list-info', props: ['student'], data () { return { dialogFormVisible: false } }, methods: { doConfirm (student) { this.$emit('confirm', student) this.dialogFormVisible = false } } } </script> <style scoped> </style>
student-list
頁面須要注意的只有這裏:
<student-list-info style="float: left" @confirm="onConfirm"
:student="this.student"
></student-list-info>code
:student
:中的student
對應props: ['student']
中的student
component
@confirm
:@後面的confirm
對應this.$emit('confirm', this.form)
中的confirm
orm
完整代碼:
<template> <div> <el-row> <student-list-info style="float: left" @confirm="onConfirm" :student="this.student"></student-list-info> </el-row> <hr> <h3>學員列表</h3> <el-table :data="tableData" @row-click="onRowClick" border stripe style="width: 100%"> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="sex" label="性別" width="180"> </el-table-column> <el-table-column prop="age" label="年齡"> </el-table-column> </el-table> </div> </template> <script> import studentListInfo from './student-list-info' export default { name: 'student-list', components: { studentListInfo }, data () { return { tableData: [{ name: '張楚嵐', sex: '男', age: '23' }, { name: '馮寶寶', sex: '女', age: '99' }, { name: '趙方旭', sex: '男', age: '59' }, { name: '肖自在', sex: '男', age: '36' } ], student: { name: '', sex: '', age: 0 } } }, methods: { onConfirm (item) { this.tableData.push(item) }, onRowClick (row) { this.student = { name: row.name, sex: row.sex, age: row.age } } } } </script> <style scoped> </style>
查看功能一直沒有思路,只能先記錄一下如今的想法,拿已有的知識來實現他啦
先選中要查看的行,而後點擊按鈕展現選中的學員信息。
如今的能力真的頗有限,再加上知識面太窄,目前沒有找到好的方式能夠直接點擊行彈出表單信息而不報錯的方式,不過我相信用不了多久就能夠實現啦,1點了,累,睡啦...