vue uni-app 的set用法总结

本文介绍Vue.js中如何正确更新数组或对象数据,并确保视图能够及时响应变化。通过使用Vue.set方法,可以解决直接修改数组元素导致视图不更新的问题,确保应用程序的实时性和准确性。
<template>
    <div id="app2">
        <p v-for="item in items" :key="item.id">{{item.message}}</p>
        <button class="btn" @click="handClick()">更改数据</button>    
    </div>
</template>

<script>
export default {
    data() {
        return {
            items: [
                { message: "one", id: "1" },
                { message: "two", id: "2" },
                { message: "three", id: "3" }
            ]
        };
    },
    mounted(){
        this.items[0]={message:"测试",id:"4"};  //此时对象的值更改了,但是视图没有更新
        this.$set(this.items,0,{message:"测试",id:"4"}); //$set可以触发更新视图
        console.log(this.items)
    },
    methods: {
        // 调用方法:Vue.set( target, key, value )

        // target:要更改的数据源(可以是对象或者数组)

        // key:要更改的具体数据

        // value :重新赋的值
        handClick() {
            //Vue methods中的this 指向的是Vue的实例,这里可以直接在this中找到items
            this.$set(this.items, 0, { message: "更改one的值", id: "0" });
        },
    }
};
</script>

<style>
</style>
Vue3 中使用 uni-app 实现登录功能,可以充分利用 Vue3 的响应式系统以及 uni-app 的跨平台能力,快速构建功能完整的登录模块。以下是一个完整的实现流程和代码示例: ### 登录页面结构 创建一个登录页面组件 `LoginPage.vue`,包含手机号和密码输入框、登录按钮以及表单校验逻辑。 ```vue <template> <view class="login-container"> <input v-model="phone" placeholder="请输入手机号" /> <input v-model="password" placeholder="请输入密码" type="password" /> <button @click="doLogin">登录</button> </view> </template> <script setup> import { ref } from 'vue'; import { login } from '@/api/userApi'; // 假设这是你的登录接口封装 const phone = ref(''); const password = ref(''); const doLogin = async () => { if (!phone.value || !password.value) { uni.showToast({ title: '手机号或密码不能为空', icon: 'none', duration: 2000, }); return; } try { const response = await login({ phone: phone.value, password: password.value }); if (response.code === 200) { uni.setStorageSync('token', response.token); // 存储 token uni.setStorageSync('user', response.user); // 存储用户信息 uni.switchTab({ url: '/pages/home/index', // 登录成功跳转首页 }); } else { uni.showToast({ title: response.message, icon: 'none', duration: 2000, }); } } catch (error) { uni.showToast({ title: '登录失败,请稍后再试', icon: 'none', duration: 2000, }); } }; </script> <style scoped> .login-container { padding: 20px; } input { margin-bottom: 15px; padding: 10px; border: 1px solid #ccc; } </style> ``` ### 登录接口封装(可选) 创建一个 `api/userApi.js` 文件,封装登录请求: ```javascript import axios from 'axios'; const apiClient = axios.create({ baseURL: 'https://your-api-endpoint.com/api', timeout: 5000, }); export const login = async (data) => { const response = await apiClient.post('/login', data); return response.data; }; ``` ### Vuex 状态管理(可选) 如果你使用 Vuex 来管理登录状态,可以在 `store/index.js` 中定义相关状态和 mutation: ```javascript import { createStore } from 'vuex'; const store = createStore({ state: { isLogin: false, userInfo: null, }, mutations: { setLogin(state, status) { state.isLogin = status; }, setUserInfo(state, info) { state.userInfo = info; }, }, actions: { login({ commit }, userInfo) { commit('setLogin', true); commit('setUserInfo', userInfo); }, logout({ commit }) { commit('setLogin', false); commit('setUserInfo', null); }, }, }); export default store; ``` 在 `main.js` 中引入并使用该 store: ```javascript import { createApp } from 'vue'; import store from './store'; import App from './App.vue'; const app = createApp(App); app.use(store); app.mount('#app'); ``` ### 登录逻辑整合 在登录成功后,除了跳转页面,还可以通过 `store` 更新全局状态: ```javascript store.dispatch('login', response.user); ``` ### 登录页面注意事项 - **表单校验**:确保手机号和密码不为空,且符合格式要求。 - **错误处理**:网络请求失败时应给出友好提示。 - **数据持久化**:使用 `uni.setStorageSync` 存储登录凭证或用户信息。 - **页面跳转**:登录成功后跳转至主页面,如使用 `uni.switchTab` 跳转 Tab 页面。 ### 安全性建议 - 密码字段建议使用加密传输。 - 登录接口建议使用 HTTPS。 - 登录凭证(如 token)应设置合理的过期时间。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值