This is a common error in JS development. Attributing a variable with a value of null or undefined generates an error. Such as:

<! -- a = {}; --> <text>{{ a.b.c }}</text> <! -- Error: Cannot read property 'c' of undefined -->

Solution 1, && method, through the execution order of logical operations to avoid errors. The code is as follows:


{{a&&a.b&&a.b.c}}

2. Add function methods to the ViewModel

For option 2, create a checkEmpty function on the ViewModel. The sample code is as follows:

export default { checkEmpty(... args) { let ret if (args.length > 0) { ret = args.shift() let tmp while (ret && args.length > 0) { tmp = args.shift() ret = ret[tmp] } } return ret || false } }

In this way, can be convenient to call.

<text>{{checkEmpty(a, 'b', 'c')}}</text>

The original link: https://developer.huawei.com/… Author: Mayism