账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue methods中方法写法问题?
    26
    0
    methods: {
        setCameraPostion: (()=>{
            let _eye = new Vector3();
            let eyeDirection = new Vector3();
            let target = new Vector3();
            let distance, newDistance;
            return function setCameraPostion(args) {
                _eye.copy(args.pos).sub(target);
                eyeDirection.copy(_eye).normalize();
                distance = _eye.length();
                newDistance = ...(有点长,略过);
                eyeDirection.setLength(newDistance).add(target);
                return eyeDirection;
            }
        })(),
    }

    以上写法是可以执行的,我想问得是这种写法是否可取?需不需要优化?
    个人感觉这种辅助变量写在data里很别扭...强迫症了快
    但缺点是无法使用this——不知道怎么用
    谢谢

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 心罪〃 普通会员 1楼

      在 Vue.js 中,您可以使用 this 关键字来引用当前对象的属性。例如,如果您有一个名为 user 的对象,您可以使用以下方法来获取用户的 ID:

      javascript this.user.id

      如果您想使用 this 关键字来引用当前组件实例的属性,可以使用 this.$parentthis.$el

      javascript this.$parent.user.id this.$el.id

      在 Vue.js 中,您还可以使用 this.$emit 方法来触发事件。例如,您可以使用以下方法来触发一个名为 updateProfile 的事件:

      javascript this.$emit('updateProfile')

      如果您想使用 this 关键字来引用事件处理函数的参数,可以使用箭头函数。

      javascript this.$emit('updateProfile', { name: 'John', age: 30 })

      在 Vue.js 中,您还可以使用 this.$data 来访问组件实例的属性。例如,您可以使用以下方法来获取组件实例的 ID:

      javascript this.$data.id

      如果您想使用 this 关键字来引用组件实例的属性,可以使用箭头函数。

      javascript this.$data.id

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部