• vue中的filters的用法


    1、效果

    金额保留两位小数,并加上单位元

    2、index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
    
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0">
        <title>Title</title>
        <link rel="stylesheet" href="css/index.css">
    </head>
    <body>
    <div id="app">
    <!--<h2>{{title}}</h2>-->
        <li v-for="(item,index) in productList">
          
            <div>金额:{{item.productPrice*item.productQuentity  | formatMoney}}</div>

    <!--注意元需要用双引号,不能用单引号,会出错--> <div>金额:{{item.productPrice*item.productQuentity | money("元")}}</div> </li> </div> <script src="js/lib/vue.min.js"></script> <script src="js/lib/vue-resource.min.js"></script> <script src="js/cart.js"></script> </body> </html>

    3、cart.js

    /**
     * Created by kk on 2017/4/16.
     */
    new Vue({
        el:"#app",
        data:{
            // title:"hello vue"
            totalMoney:0,
            productList:[]
        },
        filters:{
    formatMoney:function (value) {
        return "¥"+value.toFixed(2)
    }
        },
        mounted:function () {
            //类似于jquery中的ready方法
            this.$nextTick(function () {
                this.cartView();
            })
    
        },
        methods:{
            cartView:function () {
        // this.title="Vue hello"
                //var _this=this;
                // this.$http.get("data/cart.json",{"id":123}).then(function (res) {
                //     _this.productList=res.body.result. productList;
                //     _this.totalMoney=res.body.result.totalMoney;
                // });
    //            这里使用了ES6语法=>将this指向外部,不用再使用_this
            let _this=this;
    this.$http.get("data/cart.json",{"id":123}).then(res=> { this.productList=res.body.result. productList; this.totalMoney=res.body.result.totalMoney; }); } } });
    <!--注意Vue要大写v,不然会报错--> Vue.filter(
    "money",function (value,type) { return "¥"+value.toFixed(2)+type; });
  • 相关阅读:
    体验极佳的程序
    如何修改文档等系统文件的位置
    Demo
    Spring Boot与检索/ElasticSearch
    Java NIO:NIO概述
    Centos7 配置静态IP并使用xshell远程连接
    宏定义能否被赋值
    Centos7没有ETH0网卡
    Bringing up interface eth0: Device eth0 does not seem to be presen
    Git 常用命令
  • 原文地址:https://www.cnblogs.com/hongmaju/p/6725166.html
Copyright © 2020-2023  润新知