vue.js中,什么时候用methods?什么时候用computed?什么时候用watch?

vue.js中,什么时候用methods?什么时候用computed?什么时候用watch?,第1张

1、methods是个方法,比如你点击事件要执行一个李源铅方法,这时候就用methods,

2、computed是计算属性,实时响应的裂哗,比如你要根据data里一个值随时变化做出一些处理,就用computed。

3、举一个例子帮助哪好理解:

1)<div id="root">;

2) </div>;

3) <script>       var vm = new Vue({            el: '#root',            data:data,            methods:{

4)method_now(){                    return Date.now()} },          

5)computed:{              

6)computed_now: function () {                    return Date.now()}} })

7)</script>

4、控制台访问:

1)$vm0.computed_now;

2)1491741921719$vm0.computed_now;

3)1491741921719$vm0.computed_now;

4)1491741921719$vm0.computed_now;

5)1491741921719$vm0.method_now;

6)()1491741949941$vm0.method_now;

7)()1491741950734$vm0.method_now;

8)()1491741951445$vm0.method_now;

9)()1491741952117。

5、methods是方法和原生js没区别,大多是需要我们主动调用(比如事件)。

6、computed是get 这个get有点特殊,只要触发所依赖数据的set会自动触发get。我们只关心get的return set由系统触发或者依赖的数据触发,官方说依赖缓存只是为了理解。其实Date.now()这种只是系统不能触发set,不能触发set get当然不会通知观察者。

7、watch 是set 由data触发,我们可以在set里进行自己的条件封装。

method:

方法;信档办法;措施;条理;有条不紊。

例句:

a

reliable/effective/scientific

method

of

data

analysis

翻译:

可靠的

/

有效的

/

科学的数据分析方法。

例句:The

new

method

is

not

perfect

even

so,

it's

much

better

than

the

old

one.

翻译:新方法并不完美,就算如此,它还是比老滑丛乱方法好得多。

拓展资料

1、

a

new

method

of

solving

the

problem

解决问题的郑禅新方法

2、

traditional/alternative

methods

传统的

/

另类的方式

3、the

best

method

for

arriving

at

an

accurate

prediction

of

the

costs

准确预测成本的最佳方法

<view>父组件msg的值:{{msg}}</view>

<Header msg="{{msg}}" bindchildChange="change" ></Header>

<block wx:for="{{list}}" wx:key="index">

<ListItem rItem="{{item}}" bindchildGO="childGO"></ListItem>

</block>

<Header  msg="{{msg}}"></Header>

/* pages/list/list.wxss */

.item{

    padding: 5px

}

img1{

    width: 120px

    height: 120px

  尘蚂辩  border-radius: 5px

}

.row{

    flex: 1

    height: 120px

}

.title{

    padding: 10px

}

.dec{

    padding:0 10px

}

// pages/list/list.js

Page({

    /**

     * 页面的初始数据

     */

    data: {

        msg:"你是我的小宝贝",

     list:[{

         url:"https://www.baidu.com",

         imageUrl:"https://img1.baidu.com/it/u=2519912129,4264910682&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",

         title:"我的小可爱",

         content:"可爱可爱"

     },

     {

    物段     imageUrl:"https://img1.baidu.com/it/u=931545919,4030748642&fm=253&fmt=auto&app=138&f=JPEG?w=306&h=459",

         title:"我的小可爱",

         content:"mao可爱可爱"

     },]

    },

    change:function(str){

        this.setData({

            msg:str.detail

        })

    },

    childGO(e){

        wx.navigateTo({

          url: '/pages/webpage/webpage?url='+e.detail,

        })

    },

    /**

     * 生命周期函数--监听页面加载

     */

    onLoad: function (options) {

    },

    /派缺**

     * 生命周期函数--监听页面初次渲染完成

     */

    onReady: function () {

    },

    /**

     * 生命周期函数--监听页面显示

     */

    onShow: function () {

    },

    /**

     * 生命周期函数--监听页面隐藏

     */

    onHide: function () {

    },

    /**

     * 生命周期函数--监听页面卸载

     */

    onUnload: function () {

    },

    /**

     * 页面相关事件处理函数--监听用户下拉动作

     */

    onPullDownRefresh: function () {

    },

    /**

     * 页面上拉触底事件的处理函数

     */

    onReachBottom: function () {

    },

    /**

     * 用户点击右上角分享

     */

    onShareAppMessage: function () {

    }

})

 "usingComponents": {

    "ListItem":"/components/ListItem/ListItem",

    "Header":"/components/Header/Header"

  }

<view class="flex item" bindtap="go" data-url="{{rItem.url}}">

<image class="img1" src="{{rItem.imageUrl}}"></image>

<view class="row">

<view class="title">{{rItem.title}}</view>

<view class="dec">{{rItem.content}}</view>

</view>

</view>

.img1{

  width: 120px

  height: 120px

  display: block

  border-radius: 5px

}

.item{

  padding: 5px

}

.row{

  flex: 1

  height: 120px

}

.title{

  padding: 10px

}

.dec{

  padding: 0 10px

}

.flex{

  display: flex

}

.flex-between{

  justify-content: space-between

}

.flex-center{

  justify-content: center

  align-items: center

}

// components/ListItem/ListItem.js

Component({

  /**

   * 组件的属性列表

   */

  properties: {

    /* 子组件用properties来接收对象 */

    rItem:{

      /* 如果没有传入对象显示的默认值 */

      type:Object,

      value:{

        imageUrl:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-a7c5da54b8008049fe43089752c74ce2_r.jpg%3Fsource%3D1940ef5c&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648446806&t=9f16e5a2c12d51ba32169795e4d339f7',

      title:'我的小可爱',

      content:'我的小可爱我的小可爱我的小可爱我的小可爱'}

    }

  },

  /**

   * 组件的初始数据

   */

  data: {

  },

  /**

   * 组件的方法列表

   */

  methods: {

   go(e){

    console.log(e)

    let url = e.currentTarget.dataset.url

    this.triggerEvent('childGO',url)

   }

  }

})

<view class="t" bindtap="handler" >{{msg}}</view>

.t{

    font-size: 25px

    padding: 10px

    text-align: center

}

// components/Header/Header.js

Component({

    /**

     * 组件的属性列表

     */

    properties: {

    msg:{

        type:String,

        value:'我是Header'

    }

    },

    /**

     * 组件的初始数据

     */

    data: {

    },

    /**

     * 组件的方法列表

     */

    methods: {

    handler:function(){

        /* 直接改子组件里的msg父组件不会同步所以不能直接改 */

        /* this.setData({

            msg:'你好'

        }) */

        this.triggerEvent('childChange','你可真坏啊')

    },

    }

})

<button bindtap="getuserInfo" plain type="primary">获取用户信息</button>

<image src="{{touxiang}}" class="t"></image>

<view>{{nicheng}}</view>

<!-- 想要已进入页面就显示头像和昵称使用 open-type 标签 -->

<open-data type="userAvatarUrl"></open-data>

<open-data type="userNickName"></open-data>

<!-- 使用语言 -->

<open-data type="userLanguage"></open-data>

data: {

        msg: 'csgo',

        touxiang: "",

        nicheng: ""

    },

getuserInfo() {

        wx.getUserProfile({

            desc: '亲爱的宝贝', // 声明获取用户个人信息后的用途,后续会展示在d窗中,请谨慎填写

            success: (res) => {

                console.log(res)

                let {

                    userInfo: {

                        avatarUrl,

                        nickName

                    }

                } = res

                this.setData({

                    nicheng: nickName,

                    touxiang: avatarUrl

                })

            }

        })

    },


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12548008.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-26
下一篇2025-08-26

发表评论

登录后才能评论

评论列表(0条)

    保存