# 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'
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)
</script>
<style lang="scss" scoped>
.container {
width: 100%;
height: 300px;
border: 1px solid #333;
// 深度选择器
::v-deep .el-button {
width: 100px;
}
}
</style>
# 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
# 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
this.commit('user/setToken', data.token)
}
}
}
快捷获取值 getters
const getters = {
token: state => state.user.token
}
在入口文件index 中注册 user 模块及 getters
import { createStore } from 'vuex'
import user from './modules/user.js'
import getters from './getters'
export default createStore({
getters,
modules: {
user
}
})
在页面中使用
<script setup>
import { useStore } from 'vuex'
let store = userStore()
console.log(store.getters.token)
store.dispatch('user/login', loginForm.value)
</script>
# main.js 全局注册
<template>
<router-view></router-view>
</template>
<script>
export default {
name: 'App',
}
</script>
<style lang="scss" scoped></style>
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')
|