需要避免Vue的一些常見(jiàn)問(wèn)題,才能保證Vue應(yīng)用的性能和穩(wěn)定性。以下是一些預(yù)防措施:
//1. 避免多個(gè)正在進(jìn)行的請(qǐng)求 import axios from 'axios' let pendingRequests = [] const stopPendingRequests = () =>{ pendingRequests.forEach(request =>{ request.cancel() }) pendingRequests = [] } const apiClient = axios.create({ baseURL: 'http://myapi.com/api/v1', headers: { Authorization: `Bearer ${localStorage.getItem('token')}` } }) apiClient.interceptors.request.use(config =>{ config.cancelToken = new axios.CancelToken(cancel =>{ pendingRequests.push({ cancel }) }) stopPendingRequests() return config }) //2. 避免使用v-for和v-if一起{{ user.name }}{{ item.name }}
//3. 避免在computed或watcher中使用異步 computed: { fullName() { return `${this.firstName} ${this.lastName}` } } //4. 避免在v-if和v-show中使用非數(shù)據(jù)的計(jì)算Content