如何修复V-Model的选择选项Onchange返回先前选择或成为静态值,而不是使用vue.js的当前值?

发布于 2025-02-07 02:54:02 字数 1331 浏览 4 评论 0原文

当我访问消息页面并选择设备时,我有问题,V-Model选择值将会更改。但是,如果我访问“设备”页面并联系页面,则V-Model所选值不会更改或设置为我选择的最后一个时间值。

这是我在devicecontroller中的功能:以获取设备:

public function devices()
{   
    try {
        $devices = Device::orderby('id', 'asc')->get();
        return response()->json($devices);
    } catch (\Throwable $th) {
        return response()->json(['message' => $th->getMessage()]);
    }
}

这是从devicecontroller中获取decection数据的方法的功能:

getDevices() {
    axios.get(`/api/devices`)
      .then(response => {
          this.devices = response.data;
      });
},

这是我的选择选项代码:

<select class="form-select form-select-lg mb-3" v-model="choosed" @change="onChange()">
    <option :value="null">Choose Device to Send SMS</option>
    <option v-for="item in devices" :key="item.id" :value="item.id" >{{ item.device_name }} 
    </option>
</select>

这是我所选的V-Model和设备JSON,该设备设备该项目。ID来自数据:

export default {
    data() {
        return {
            devices: {}, 
            choosed: null,
        }
    },
}

这是我的onChange in thate in方法:

onChange: function(){
    this.choosed = this.item.id;
},

I have a problem when I visit the message page and I select a device, the v-model selected value will change. But if I visit the device page and contact page the v-model selected value will not change or become set to the last time value that I selected.

Here is my function in DeviceController to fetch devices:

public function devices()
{   
    try {
        $devices = Device::orderby('id', 'asc')->get();
        return response()->json($devices);
    } catch (\Throwable $th) {
        return response()->json(['message' => $th->getMessage()]);
    }
}

Here is the function from method to get devices{} data from DeviceController:

getDevices() {
    axios.get(`/api/devices`)
      .then(response => {
          this.devices = response.data;
      });
},

Here is my select option code:

<select class="form-select form-select-lg mb-3" v-model="choosed" @change="onChange()">
    <option :value="null">Choose Device to Send SMS</option>
    <option v-for="item in devices" :key="item.id" :value="item.id" >{{ item.device_name }} 
    </option>
</select>

Here is my selected v-model and devices JSON which devices that item.id came from in data:

export default {
    data() {
        return {
            devices: {}, 
            choosed: null,
        }
    },
}

Here is my onChange function in method:

onChange: function(){
    this.choosed = this.item.id;
},

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-02-14 02:54:02

删除函数onChange - 这是显而易见的。

Remove the function onChange - it is plain wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文