美文网首页
vue的父子传值的值赋值处理导致界面数据没渲染

vue的父子传值的值赋值处理导致界面数据没渲染

作者: 尘埃里的玄 | 来源:发表于2022-09-09 11:29 被阅读0次

    现象:


    image.png

    在vue的生命周期钩子函数中进行赋值并未生效,
    父组件

    <template>
      <div class="app-container">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>用户管理</span>
            <!--        <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>-->
          </div>
          <el-form ref="form" :inline="true" :model="searchForm" @submit.native.prevent>
            <el-form-item label="所属网站" prop="gatherId">
              <el-select
                v-model="searchForm.value"
                style="width:375px !important"
                filterable
                clearable
                remote
                reserve-keyword
                placeholder="请输入网址关键词"
                :remote-method="remoteMethod"
              >
                <el-option
                  v-for="item in options"
                  :key="item.urlId"
                  :label="item.url"
                  :value="item.urlId"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                native-type="submit"
                :loading="searchLoading"
                @click="search"
              >搜索
              </el-button>
              <el-button
                type="info"
                plain
                icon="el-icon-refresh-left"
                @click="reset"
              >重置
              </el-button>
            </el-form-item>
          </el-form>
          <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            @selection-change="handleSelectionChange"
          >
            <el-table-column
              type="selection"
              width="55"
            />
            <el-table-column align="center" label="序列" width="50">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column
              label="推荐码"
            >
              <template slot-scope="scope">{{ scope.row.date }}</template>
            </el-table-column>
            <el-table-column
              prop="name"
              label="用户角色"
            />
            <el-table-column
              prop="address"
              label="上级"
              show-overflow-tooltip
            />
            <el-table-column
              prop="address"
              label="用户名"
              show-overflow-tooltip
            />
            <el-table-column
              prop="address"
              label="email"
              show-overflow-tooltip
            />
            <el-table-column
              prop="address"
              label="登录次数"
              show-overflow-tooltip
            />
            <el-table-column
              prop="address"
              label="登录地点"
              show-overflow-tooltip
            />
            <el-table-column
              prop="address"
              label="登录IP"
              show-overflow-tooltip
            />
            <el-table-column
              prop="address"
              label="日期"
              show-overflow-tooltip
            />
            <el-table-column label="操作" align="center" width="200">
              <template slot-scope="scope">
                <el-button
                  type="info"
                  icon="el-icon-edit"
                  native-type="submit"
                  size="mini"
                  @click="edit(scope.row)"
                >编辑
                </el-button>
                <el-button
                  type="info"
                  icon="el-icon-delete"
                  native-type="submit"
                  size="mini"
                  @click="examine(scope.row.urlId)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="myPagination">
            <el-pagination
              hide-on-single-page
              background
              :current-page.sync="currentPage"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-card>
        <user-edit ref="userEditDialog" :use-data="selectData" />
      </div>
    </template>
    <style>
    .myPagination {
      display: flex;
      margin-top: 15px;
      justify-content: flex-end;
    }
    </style>
    <script>
    
    import { getAccessReport, getUrl } from '@/api/user'
    import UserEdit from '@/views/user/userEdit'
    
    var _self
    export default {
      name: 'User',
      components: { UserEdit },
      data() {
        return {
          tableData: [{
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区'
          }, {
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区'
          }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区'
          }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区'
          }, {
            date: '2016-05-08',
            name: '王小虎',
            address: '上海市普陀区'
          }, {
            date: '2016-05-06',
            name: '王小虎',
            address: '上海市普陀区'
          }, {
            date: '2016-05-07',
            name: '王小虎',
            address: '上海市普陀区'
          }], // 表格内容
          multipleSelection: [], // 多选内容
          listLoading: false, // 表格加载等待
          searchLoading: false, // 搜索等待
          getUrlloading: false, // 网站搜索加载等待
          options: [], // 搜索下拉框内容
          total: 0, // 总条数
          pageSize: 10, // 每页条数
          currentPage: 1, // 页码
          searchForm: {
            value: null, // url地址
            lastTime: []// 起止时间
          },
          selectData: {} // 选中的该行数据(用于编辑弹窗)
        }
      },
      created() {
        _self = this
        _self.getList()
      },
      methods: {
        // 下拉搜索网址
        remoteMethod(query) {
          if (query !== '') {
            _self.getUrlloading = true
            const params = {
              'url': query
            }
            getUrl(params).then(res => {
              if (res.code == 0) {
                _self.options = res.data
              }
            }).finally(() => {
              _self.getUrlloading = false
            })
          } else {
            _self.options = []
          }
        },
        // 搜索table
        search() {
          // if (_self.searchForm.value != null || _self.searchForm.lastTime[0] != undefined) {
          //   console.log(_self.searchForm)
          //   _self.getList()// 获取表格数据
          // } else {
          //   this.$message.error('请选择搜索内容')
          // }
          _self.getList()// 获取表格数据
        },
        // 重置事件
        reset() {
          _self.searchForm.value = ''
          _self.searchForm.lastTime = []
          _self.urlId = ''
          _self.pageSize = 10
          _self.currentPage = 1
          _self.getList()
        },
        // 获取table数据
        getList() {
          _self.listLoading = true
          const params = {
            'startTime': _self.searchForm.lastTime[0] == undefined ? '' : _self.searchForm.lastTime[0],
            'endTime': _self.searchForm.lastTime[1] == undefined ? '' : _self.searchForm.lastTime[1],
            'urlId': _self.searchForm.value,
            'pageNum': _self.currentPage,
            'pageSize': _self.pageSize
          }
          getAccessReport(params).then(res => {
            if (res.code == 0) {
              _self.tableList = res.data.rows
              _self.total = res.data.total
              _self.pageSize = res.data.pageSize
              _self.currentPage = res.data.pageNum
            }
          }).catch(err => {
            console.log(err)
          }).finally(() => {
            _self.listLoading = false
          })
        },
        // 条数pagesize切换事件
        handleSizeChange(val) {
          this.pageSize = val
          this.getList()
        },
        handleCurrentChange(val) {
          this.currentPage = val
          this.getList()
        },
        // 详情 跳转按钮
        examine(listId) {
          _self.$router.push({
            path: 'table',
            query: {
              listId: listId,
              times: _self.searchForm.lastTime
            }
          })
        },
        // 打开编辑弹窗
        edit(data) {
          this.$refs['userEditDialog'].dialogVisible = true
          this.selectData = data
        },
        // 处理选择的
        handleSelectionChange(val) {
          this.multipleSelection = val
        }
      }
    }
    </script>
    
    <style scoped>
    
    </style>
    
    

    子组件:

    <template>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
          <el-form-item label="用户名">
            <el-input v-model="formLabelAlign.date" />
          </el-form-item>
          <el-form-item label="上级">
            <el-input v-model="formLabelAlign.name" />
          </el-form-item>
          <el-form-item label="用户角色">
            <el-input v-model="formLabelAlign.address" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          {{ useData }}
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </template>
    
    <script>
    export default {
      name: 'UserEdit',
      props: {
        useData: Object
      },
      data() {
        return {
          dialogVisible: false,
          labelPosition: 'right',
          formLabelAlign: {
            date: '',
            name: '',
            address: ''
          }
        }
      },
      watch: {
        useData(newVal) {
          this.formLabelAlign = newVal
        }
      },
      methods: {
        created() {
          console.log(this.useData)
          this.formLabelAlign = this.useData
        },
        handleClose(done) {
          this.$confirm('确认关闭?')
            .then(_ => {
              done()
            })
            .catch(_ => {})
        }
      }
    }
    </script>
    
    <style scoped>
    
    </style>
    
    

    需要把代码改成watch赋值,对于props的变量

    image.png

    相关文章

      网友评论

          本文标题:vue的父子传值的值赋值处理导致界面数据没渲染

          本文链接:https://www.haomeiwen.com/subject/zmrtortx.html