VueJS:变量在仅计算内部未定义

Eli*_*ski 9 vue.js nuxt.js

我正在尝试使用Vue,Nuxt,Axios和Buefy进行异步自动完成输入.它基本上可以工作,但是当用户刚刚开始输入时我需要有不同的字符串,而且还没有什么可以显示,并且当找不到这样的请求时.

如果输入值不为空,我正在检查计算变量,如果无法找到请求地址,axios会返回空箭头来处理.但它会导致错误

无法读取未定义的属性"长度"

奇怪的是address变量已成功用于组件的其他部分.

我的vue文件如下:

<template lang="pug">
b-field(label="Your address?")
    b-autocomplete(
    rounded,
    v-model="address",
    :data="data",
    placeholder="Start typing",
    icon="magnify",
    @input="getAsyncData",
    @select="option => selected = option",
    :loading="isFetching"
    )
        template(slot="empty") {{ dummyText }}
</template>

<script>
import axios from 'axios'
import debounce from 'lodash/debounce'

export default {
    data() {
        return {
            data: [],
            address: '',
            selected: null,
            isFetching: false,
            nothingFound: false,
            test: false
        }
    },

    computed: {
        dummyText: () => {
            if (this.address.length > 0 && this.nothingFound) { // This will return error
                return 'There is no such address'
            } else {
                return 'Keep typing'
            }
        }
    },

    methods: {
        getAsyncData: debounce(function () {
            this.isFetching = true

            axios.post('https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address', {
                "query": this.address,
                "count": 8
            }, {
                headers: {
                    'Authorization': 'Token sometoken',
                    'Content-Type': 'application/json',
                    'Accept': 'application/json',
                }
            })
                .then(response => {
                    this.isFetching = false
                    this.data = Object.values(response.data.suggestions)
                    if (response.data.suggestions.length===0) this.nothingFound = true
                    console.log(this.address.length) // This will work
                })
                .catch(error => {
                    this.isFetching = false
                    console.log(error);
                })
        }, 300)
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

这不是关于ssr,我试图在挂钩内部初始化组件.想想我错过了一些明显的东西,但我已经花了好几个小时试图解决这个问题而没有成功

Sph*_*inx 26

不要使用箭头功能()=>{}computed,它会导致错误的情况下(不是当前Vue的实例).

改为function () {}那么它应该工作正常.

因为methods,watch你应该遵循相同的规则.

computed: {
    dummyText: function () { // change to function () {}
        if (this.address.length > 0 && this.nothingFound) { // This will return error
            return 'There is no such address'
        } else {
            return 'Keep typing'
        }
    }
},
Run Code Online (Sandbox Code Playgroud)


小智 10

您还可以将 es2015 速记用于方法函数:

computed: {
    dummyText() {
        return this.address.length > 0 && this.nothingFound ? 'There is no such address' : 'Keep typing';
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

Vue文档声明不要在属性或回调上使用箭头函数。

您遇到此错误是因为箭头函数不会将 this 绑定到您为其定义计算属性的 vue 实例,因为箭头函数绑定到父上下文并且 this.address 未定义。如果您对方法使用箭头函数,也会发生同样的情况。

使用常规函数:

dummyText: function () {
    console.log(this.address)
}
Run Code Online (Sandbox Code Playgroud)

或者使用 ES5 简写:

dummyText() {
    console.log(this.address)
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您想继续使用箭头函数,您可以将组件实例(this)作为参数传递,因为计算属性接收组件实例作为其第一个参数。:

dummyText : ctx => console.log(ctx.address)
Run Code Online (Sandbox Code Playgroud)