Vue 2.0 不再支持在 v-html 中使用過濾器
解決方法:
1:全局方法(推薦)
2:computed 屬性
3:$options.filters(推薦)
1:使用全局方法:
可以在 Vue 上定義全局方法:
1 Vue.prototype.msg = function(msg){ 2 3 return msg.replace("\n","<br>") 4 5 };
然后所有地方上都可以直接用這個方法了:
1 <div v-html="msg(content)"></div>
2:computed 屬性
1 var appMain = new Vue({ 2 3 data:{ 4 5 content:"XXX" 6 7 }, 8 9 el:"#appMain", 10 11 computed:{ 12 13 content:function(msg){ 14 15 return msg.replace("\n","<br>") 16 17 } 18 19 } 20 21 })
頁面上:
1 <div>{{content}}</div>
3:$options.filters:
在定義的vue里的filter添加方法:
1 var appMain = new Vue({ 2 3 el:"#appMain", 4 5 filters:{ 6 7 msg:function(msg){ 8 9 return msg.replace(/\n/g,"<br>") 10 11 } 12 13 }, 14 15 data:{ 16 17 content:"XXXX" 18 19 } 20 21 })
然后頁面上都可以直接用這個方法了:
1 <div id="appMain"> 2 3 <div v-html="$options.filters.msg(content)"></div> 4 5 </div>
參考:https://www.cnblogs.com/rxqlx/p/10330517.html