为vue添加公用方法,vue添加通用方法
common.js
var common =function() { return{ f1:function(){ console.log("this is common f1()."); }, f2:function(){ console.log(" out vist f222222()."); }, // 小写转大写 toUppercase:function(o,parName){ if(o && o[parName]){ o[parName] = o[parName].toUpperCase(); } } }}(); if(typeof(Vue)=="function")Vue.prototype.common=common;
html页
<html> <head> <meta charset="UTF-8"> <script src="https://vuejs.org/js/vue.min.js"></script> <script src="common.js"></script> </head> <body> <div id="app"> <br>username:<input type="text" @keyup="common.toUppercase(v1,'username')" v-model="username" > {{username}} <br>userobj:<input type="text" @keyup="common.toUppercase(userobj,'usernameEN')" v-model="userobj.usernameEN" > </div> </body> </html> <script> var v1=new Vue({ el: '#app', data: { username:"", userobj:{usernameEN:""} }, created:function(){ this.common.f1(); } ,methods:{ testf2:function(){ //this.common.f2(); } } }) //在vue外面也可以调用 common.f1(); common.f2(); </script>
欢迎加入JAVA技术交流QQ群:179945282
欢迎加入ASP.NET(C#)交流QQ群:17534377