赞
踩
第一章
第二章
同一台电脑 实现 vue-cli2和vue-cli3同时并存 及 常见命令
第三章
第四章
【TypeScript】在vue3中遇到的问题及解决方案,未完待续
第五章
第六章
目录
坑千千万,踩好才有完美的框架~
一定要明确好使用哪套,两套并不是内容、特性通用!
props
被解析之后执行。它是组合式 API 的入口。- <script lang="ts">
- export default {
- setup() {
- }
- }
- </script>
整体代码
- <template>
- <div class="login-wrap">
- <div class="ms-login">
- <div class="ms-title">后台管理系统</div>
- <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="0px" class="ms-content">
- <el-form-item prop="username">
- <el-input v-model="ruleForm.username" placeholder="username">
- <template #prepend>
- <el-button icon="el-icon-user"></el-button>
- </template>
- </el-input>
- </el-form-item>
- <el-form-item prop="password">
- <el-input type="password" placeholder="password" v-model="ruleForm.password">
- <template #prepend>
- <el-button icon="el-icon-lock"></el-button>
- </template>
- </el-input>
- </el-form-item>
- <div class="login-btn">
- <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
- </div>
- <p class="login-tips">Tips : 用户名和密码随便填。</p>
- </el-form>
- </div>
- </div>
- </template>
-
- <script lang="ts">
- import {
- ref,
- reactive
- } from "vue";
- import {
- useStore
- } from "vuex";
- import {
- useRouter
- } from "vue-router";
- import {
- ElMessage
- } from "element-plus";
- import {
- roleList
- } from "../request/api.js";
-
- export default {
- setup() {
- const router = useRouter();
- const ruleForm = reactive({
- username: "admin",
- password: "123123",
- });
- const rules = {
- username: [{
- required: true,
- message: "请输入用户名",
- trigger: "blur",
- }],
- password: [{
- required: true,
- message: "请输入密码",
- trigger: "blur"
- }],
- }; // 一个普通对象,修改后不会被proxy拦截,进而页面也不会动态更新
- const ruleFormRef = ref('');
- const submitForm = async (formEl) => {
- if (!formEl) return
- formEl.validate((valid) => {
- if (valid) {
- submitFormLink();
- } else {
- ElMessage.error("登录失败");
- return false;
- }
- });
- };
- // 使用响应式函数reactive构建proxy响应式对象
- const params = reactive({
- pageNum: 1,
- pageSize: 10,
- systemFlag: 'water'
- })
- const submitFormLink = () => {
- roleList(params).then(res => {
- if (res.code == 0) {
- ElMessage({
- showClose: true,
- message: '登录成功',
- type: 'success',
- })
- // ElMessage.success('登录成功')
- localStorage.setItem("ms_username", ruleForm.username);
- router.push("/");
- } else if (res.code !== 0) {
- ElMessage.error(res.msg)
- }
- })
- };
- const store = useStore();
- store.commit("clearTags");
- // 使用时,要把对象return出去,才能在template中使用
- return {
- ruleForm,
- rules,
- ruleFormRef,
- submitForm,
- params,
- submitFormLink
- };
- },
- };
- </script>
-
- <style scoped>
- </style>
<script setup>
的时候,任何在 <script setup>
声明的顶层的绑定 (包括变量,函数声明,以及 import 引入的内容) 都能在模板中直接使用:methods
选项来暴露它setup()
函数中返回值一样,ref 值在模板中使用的时候会自动解包<script setup>
中必须使用 defineProps
和 defineEmits
API 来声明 props
和 emits
,它们具备完整的类型推断并且在 <script setup>
中是直接可用的 - <script setup>
- const props = defineProps({
- foo: String
- })
-
- const emit = defineEmits(['change', 'delete'])
- // setup code
- </script>
<script setup>
范围里的值也能被直接作为自定义组件的标签名使用- <script setup>
- import MyComponent from './MyComponent.vue'
- </script>
-
- <template>
- <MyComponent />
- </template>
动态组件:由于组件被引用为变量而不是作为字符串键来注册的,在 <script setup>
中要使用动态组件的时候,就应该使用动态的 :is
来绑定
在 <script setup>
中必须使用 defineProps
和 defineEmits
API 来声明 props
和 emits
,它们具备完整的类型推断并且在 <script setup>
中是直接可用的
本登录页面使用
- <script lang="ts" setup>
- </script>
整体代码
- <template>
- <div class="login-wrap">
- <div class="ms-login">
- <div class="ms-title">德润厚天项目管理系统</div>
- <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="0px" class="ms-content">
- <el-form-item prop="username">
- <el-input v-model="ruleForm.username" placeholder="username">
- <template #prepend>
- <el-button icon="el-icon-user"></el-button>
- </template>
- </el-input>
- </el-form-item>
- <el-form-item prop="password">
- <el-input type="password" placeholder="password" v-model="ruleForm.password">
- <template #prepend>
- <el-button icon="el-icon-lock"></el-button>
- </template>
- </el-input>
- </el-form-item>
- <div class="login-btn">
- <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
- </div>
- <p class="login-tips">Tips : 用户名和密码随便填。</p>
- </el-form>
- </div>
- </div>
- </template>
-
- <script lang="ts" setup>
- import {
- ref,
- reactive
- } from "vue";
- import {
- useStore
- } from "vuex";
- import {
- useRouter
- } from "vue-router";
- import {
- ElMessage
- } from "element-plus";
- import {
- roleList
- } from "../../request/api.js";
-
- const router = useRouter();
- const ruleForm = reactive({
- username: "admin",
- password: "123123",
- });
- const rules = {
- username: [{
- required: true,
- message: "请输入用户名",
- trigger: "blur",
- }],
- password: [{
- required: true,
- message: "请输入密码",
- trigger: "blur"
- }],
- }; // 一个普通对象,修改后不会被proxy拦截,进而页面也不会动态更新
- const ruleFormRef = ref('');
- const submitForm = async (formEl) => {
- if (!formEl) return
- formEl.validate((valid) => {
- if (valid) {
- submitFormLinkFree();
- } else {
- ElMessage.error("登录失败");
- return false;
- }
- });
- };
- // 使用响应式函数reactive构建proxy响应式对象
- const params = reactive({
- pageNum: 1,
- pageSize: 10,
- systemFlag: 'water'
- })
- const submitFormLink = () => {
- roleList(params).then(res => {
- if (res.code == 0) {
- ElMessage({
- showClose: true,
- message: '登录成功',
- type: 'success',
- })
- // ElMessage.success('登录成功')
- localStorage.setItem("ms_username", ruleForm.username);
- router.push("/");
- } else if (res.code !== 0) {
- ElMessage.error(res.msg)
- }
- })
- };
- const submitFormLinkFree = () => {
- ElMessage({
- showClose: true,
- message: '登录成功',
- type: 'success',
- })
- localStorage.setItem("ms_username", ruleForm.username);
- router.push("/");
- };
- const store = useStore();
- store.commit("clearTags");
- </script>
-
- <style scoped>
- .login-wrap {
- position: relative;
- width: 100%;
- height: 100%;
- background: url(../../assets/img/login-bg.jpg) no-repeat;
- background-size: 100% 100%;
- }
-
- .ms-title {
- width: 100%;
- line-height: 50px;
- text-align: center;
- font-size: 20px;
- color: #fff;
- border-bottom: 1px solid #ddd;
- }
-
- .ms-login {
- position: absolute;
- left: 50%;
- top: 50%;
- width: 350px;
- margin: -190px 0 0 -175px;
- border-radius: 5px;
- background: rgba(255, 255, 255, 0.3);
- overflow: hidden;
- }
-
- .ms-content {
- padding: 30px 30px;
- }
-
- .login-btn {
- text-align: center;
- }
-
- .login-btn button {
- width: 100%;
- height: 36px;
- margin-bottom: 10px;
- }
-
- .login-tips {
- font-size: 12px;
- line-height: 30px;
- color: #fff;
- }
- </style>
以上就是今天要讲的内容,本文仅仅简单介绍了登录页面的使用,未完待续。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。