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>