• vue3基本使用笔记(更新中)


    # vue

    <template>
       <div class="container">
           <el-input v-model="loginForm.username"></el-input>
           <el-button @click="getPropsValue"></el-button> 
       </div>
    </template>
    <script setup>
    import { defineProps, computed, ref } from 'vue'
    // props 操作
    const props = defineProps({
        // 参数一
        icon: {
            type: String, // 类型
            required: true, // 必填项
        },
        // 参数二
        list: {
            type: Array,
            default: [] // 设置默认值
        }
    })
    const getPropsValue = () => {
        console.log(props.icon)
        console.log(props.list)
    }  
    
    
    // 计算属性
    const isExternal = computed(() => {
        // 判断是否为外部链接
        return /^(https?:|mailto:|tel:)/.test(props.icon)
    })
    
    
    // 创建响应式变量
    // 基本数据类型
    const passwordType = ref('password') 
    console.log(passwordType.value) 
    // 引用数据类型   
    const loginForm = ref({
      username: 'admin',
      password: '123456'
    }) 
    console.log(loginForm.value) // {username: 'admin', password: '123456'}
    </script>
    <style lang="scss" scoped>
        .container {
            width: 100%;
            height: 300px;
            border: 1px solid #333;
            // 深度选择器
            ::v-deep .el-button {
                width: 100px;
            }
        }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    # vue-router

    import { createRouter, createWebHashHistory } from 'vue-router'
    
    const publicRoutes = [
      {
        path: '/login',
        component: () => import('@/views/login/index')
      },
      {
        path: '/',
        component: () => import('@/layout/index')
      }
    ]
    
    const router = createRouter({
      history: createWebHashHistory(),
      routes: publicRoutes
    })
    
    export default router
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    # vuex

    user 模块

    export default {
      namespaced: true, // 该模块为单独的模块
      state: () => ({
        token: 'c3551e1c-9cc0-4365-83cc-c7eda0b10e01'
      }),
      mutations: {
        setToken (state, token) {
          state.token = token
          setItem(TOKEN, token)
        }
      },
      actions: { // 行为
        // 登录
        login (context, userInfo) {
          const { username, password } = userInfo
          // to do 登录操作
          
          // 登录成功: 保存 token
          this.commit('user/setToken', data.token)
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    快捷获取值 getters

    const getters = {
      token: state => state.user.token
    }
    
    • 1
    • 2
    • 3

    在入口文件index中注册 user 模块及 getters

    import { createStore } from 'vuex'
    import user from './modules/user.js'
    import getters from './getters'
    export default createStore({
      getters,
      modules: {
        user
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在页面中使用

    <script setup>
    import { useStore } from 'vuex'
    // 获取 store 实例
    let store = userStore()
    // 获取 token
    console.log(store.getters.token)  
    // 调用 user 模块中 actions 的 login 方法
    store.dispatch('user/login', loginForm.value)
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    # main.js 全局注册

    <template>
       <router-view></router-view>
    </template>
    <script>
    export default {
      name: 'App',
    }
    </script>
    <style lang="scss" scoped></style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    import { createApp } from 'vue'
    import App from './App.vue'
    import router from './router'
    import store from './store'
    const app = createApp(App)
    app.use(store).use(router).mount('#app')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Mac电脑无法识别移动硬盘怎么办?
    数字通信测量仪器5201数据网络测试仪
    Bun v1.0.3 发布,Zig 编写的 JavaScript 运行时
    低代码开发:智能财务系统开发应用
    【JVM】类加载机制:分析与验证
    [激光原理与应用-15]:《激光原理与技术》-1- 什么是激光,激光概述
    【统计任意一组字符中大小写字母的个数】
    智能无人驿站系统小程序
    关于mybatis中Mapper对应xml要写参数名的
    好的FPGA编码风格(1)--尽量避免组合逻辑环路(Combinational Loops)
  • 原文地址:https://blog.csdn.net/weixin_44659458/article/details/125471617