比如請求數(shù)據(jù)返回金額為10000哎壳,然后我們給做個金額格式分段;
1尚卫、我們首先模擬個數(shù)據(jù)~vue:data( ?){ reutrn:{ num:10000? } } ? ? ? ? ? ? ?原生js:var num=10000;
2归榕、在項目中創(chuàng)建一個.js文件寫入js方法,如下吱涉;
Number.prototype.formatMoney = function (places, thousand, decimal) {
? places = !isNaN(places = Math.abs(places)) ? places : 2;
? thousand = thousand || ",";
? decimal = decimal || ".";
? var number = this,
? ? negative = number < 0 ? "-" : "",
? ? i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
? ? j = (j = i.length) > 3 ? j % 3 : 0;
? return negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
};
3刹泄、js使用可以直接寫在方法外 ? ?
console.log(?num.formatMoney() )
4、vue可以將.js文件引入main文件怎爵,或者在本頁面引入
import '../../assets/js/currency.js'
5特石、寫入要轉(zhuǎn)換的變量值
console.log( this.num.formatMoney() )