vue2.0版本中v-html中過濾器的使用


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM