vue中的slot-scope及scope.row用法

slot-scope及scope.row的用法

最近在寫後臺管理系統 在寫到修改的地方時 之前的思路是直接把當前對象傳過去 然後在進行修改

現在vue提供瞭scope 以及 scope.row 可以讓我們更方便的操作數據

  • slot-scope='scope' 作用域插槽中定義一個對象(這裡對象被定義為scope)來存儲插槽上綁定的數據的用法
  • scope.row 使用ElementUI表格模板渲染數據時使用

當前行數據的獲取也會用到插槽,scope相當於一行的數據, scope.row相當於當前行的數據對象

<el-table :data="userList" stripe style="width: 100%">
      <el-table-column prop="username"label="姓名" width="180"></el-table-column>
      <el-table-column prop="email" label="郵箱" width="180"> </el-table-column>
      <el-table-column prop="mobile" label="電話"> </el-table-column>
      <el-table-column label="用戶狀態">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.mg_state" @change="userstateChange(scope.row.id, scope.row.mg_state)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="adress" label="操作"> </el-table-column>
</el-table>

:data ==》“userList”

表格綁定瞭用於存儲數據的數組,裡面每一個元素都是數據對象

slot-scope ==》“scope”

這是作用域插槽中定義一個對象(這裡對象被定義為scope)來存儲插槽上綁定的數據的用法

當前行的數據對象 ==》 scope.row

在這裡使用ElementUI表格模板渲染數據時,當前行數據的獲取也會用到插槽,scope相當於一行的數據, scope.row相當於當前行的數據對象

還是比較方便的~

vue項目中slot-scope="scope"報錯scope is defined but never used

報錯是由於eslint的檢測機制造成的

解決方法

在template上 加上 eslint-disable-next-line 註釋即可

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: