Vue.js中如何从后端获取serviceNo并动态赋值
可以通过调用后端接口获取到serviceNo,并将其赋值给data()中的serviceNo属性。具体实现方式可以使用axios等前端库发送请求,然后在请求成功后将返回的serviceNo赋值给data()中的serviceNo属性。示例代码如下:
import axios from 'axios';
export default {
data() {
return {
get,
serviceNo: '', // 将serviceNo初始值设为空
loginToken: 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiOWIwM2E0MGNjMmJkNGNlNWIzN2FmM2M1YWNiOWZhZjAiLCJ1c2VyX2tleSI6ImM3NmQ3YzBjLTNkZWYtNDhjNy05ODI0LTBjZjRiZjcxMGE5YiIsInVzZXJuYW1lIjoiR0s0MDEyOTYyNTQifQ.fNtP29ukX5EiMEIlnox-on9gUU4Z7AEPq9cp7WFCP3PH5erggH3dQHHfKDiYp36oRHbT0s1NoUhuQD5V5m9q6A',
equipment: [
{
id: 3,
name: '步骤一',
icon: 'el-icon-postcard',
},
{
id: 1,
name: '步骤二',
icon: 'el-icon-postcard',
},
{
id: 0,
name: '步骤三',
icon: 'el-icon-camera-solid',
},
],
activeEquipmentForm: {
id: 3,
name: '高拍仪',
},
};
},
mounted() {
// 在组件挂载完成后发送请求获取serviceNo
axios.get('/get-service-no')
.then(response => {
// 将获取到的serviceNo赋值给data()中的serviceNo属性
this.serviceNo = response.data.serviceNo;
})
.catch(error => {
console.error(error);
});
}
};
其中,/get-service-no
是后端提供的获取serviceNo的接口。在请求成功后,可以通过response.data.serviceNo
获取到返回的serviceNo。如果请求失败,则会在控制台输出错误信息。
原文地址: http://www.cveoy.top/t/topic/oK2x 著作权归作者所有。请勿转载和采集!