uni-app屬性值的動態綁定

1.問題

使用傳統的方法給標籤的屬性動態綁定數據報錯。

2.錯誤重現

2.1 錯誤代碼

<image src="{{imgeSrc}}"></image>

2.2 錯誤信息

Module Error (from ./node_modules/@dcloudio/vue-cli-plugin-uni/packages/vue-loader/lib/loaders/templateLoader.js):
10:01:33.690 (Emitted value instead of an instance of Error) 
10:01:33.703   Errors compiling template:
10:01:33.704   src="{{imgeSrc}}": Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example, instead of <div id="{{ val }}">, use <div :id="val">.

2.3 錯誤原因

      因爲vue 2.x不支持對屬性使用插值{{}}的方式賦值,所以要使用v-bind指令(或簡寫“:”)來指定屬性。

3.解決方案(以image標籤講解)

  • v-bind指令

  v-bind:src="imgeSrc"

<image v-bind:src="imgeSrc"></image>
  • v-bind簡寫指令:
  :src="imgeSrc"
<image :src="imgeSrc"></image>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章