vue3 setup标签使用总结

引入element-plus

	在main.js中引用并且使用中文 顺便引入图标库
const app = createApp(App); // 生成的vue实例 已经存在不需要重复创建
...
import ElementPlus from "element-plus";
import zhCn from "element-plus/dist/locale/zh-cn.mjs"; // 中文
import * as ElementPlusIconsVue from "@element-plus/icons-vue";
// 使用icon
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.use(ElementPlus, {
  locale: zhCn,
}); // 使用element-plus

引入vuex

在sotre文件夹中创建index.js 以及其他的 moudle
import { createStore } from "vuex";
import app from "./moudle/app.js";

export default createStore({
  modules: {
    app,
  },
  state() {
    return {
      test: 111,
    };
  },
});

其中 moudle/app.js

const app = {
  state: {
    flag: false,
  },
  getters: {
    getFlag(state) {
      return state.flag
    }
  },
  mutations: {
    setFlag: (state, payload) => {
      state.flag = payload
    }
  },
  actions: {
    // 是否展开左侧导航条
    test({commit}, payload) {
      return new Promise(resolve => {
        commit('setFlag', payload)
        resolve(true)
      })
    }
  }
}

export default app

main.js中引入

const app = createApp(App); // 生成的vue实例 已经存在不需要重复创建
...
import store from './store/index'
app.use(store);

在其他页面使用

<script setup>
	import sideBarItem from "./sideBarItem.vue";
	import { reactive, computed } from "vue";
	import { useRoute } from "vue-router";
	const router = useRoute();
	
	import store from '@/store/index'
	
	const self = reactive({});
	
	const flag = computed(() => {
	  return store.getters.getFlag;
	});
	
	
	const changeFlag = () => {
	  store.dispatch("test", true);
	};
</script>

使用props

子组件:
<template>
	<p class="title" v-text="title"></p>
	<span v-if="showIcon">{{title}}</span>
</template>
<script setup>
import { defineProps, reactive } from "vue"; // 文档上面写着不需要引入 但是控制台有警告 所以干脆就引入了

const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  showIcon: {
    type: Boolean,
    default: true,
  },
});

const self = reactive({
  showContent: true,
});

const handleDisplay = () => {
  self.showContent = !self.showContent;
};
</script>

具体使用:

<template>
  <div class="test">
    <card title="test" :showIcon="false"></card>
  </div>
</template>

<script setup>
import card from "@/components/card.vue";

</script>

定义emit事件

子组件:

<template>
  <div @click="handleClick(1)">emit事件</div>
</template>

<script setup>
import { defineEmits } from "vue";

const emit = defineEmits([
  "emitClick",
]);

const handleClick = (index) => {
	emit("emitClick", index;
}

</script>

使用:

<template>
  <div class="test">2
    <card @emitClick="handleClick"></card>
  </div>
</template>

<script setup>
import card from "@/components/card.vue";

const handleClick = (index) => {
	console.log('index', index)
}
</script>

使用watch监听props

大部分监听还是差不多意思 这个稍微绕了点

<script setup>
import { watch, defineProps } from "vue";

const props = defineProps({
  item: {
    type: Object,
    required: true,
  },
});

// 监听 props 需要使用函数的方式进行返回 
watch(() => props.item, (newValue, oldValue) => {
	...
})
</script>

computed 计算函数

<script setup>
import { computed } from "vue";
import store from '@/store/index'

const keepRouterList = computed(() => {
  return store.getters.getKeepRouters;
});
</script>

refs 使用

其实习惯了之后 感觉这个改动还行 能接受

<template>
  <div>
      <el-form ref="loginForm" class="login-form">
        
      </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
const loginForm = ref(null) // 变量名是ref的名字 ref的初始值是null

const self = reactive({
	...
})

const submitForm = async () => {
  loginForm.value.validate((valid) => {
    if (valid) {    
    	...
	} else {
      console.log('error submit!!')
      return false;
    }
  })
}
const resetForm = () => {
  loginForm.value.resetFields();
}
</script>

<style scoped>
</style>

vue-router使用

先创建路由文件
import { createRouter, createWebHistory } from "vue-router";
// hash模式:createWebHashHistory
// history模式:createWebHistory

const layout = () =>
  import(/* webpackChunkName: "layout" */ "@/views/layout/layout.vue");
const dashboard = () =>
  import(/* webpackChunkName: "dashboard" */ "@/views/dashboard/index.vue");
const testDom = () =>
  import(/* webpackChunkName: "test" */ "@/views/test/index.vue");
const test2 = () =>
  import(/* webpackChunkName: "test" */ "@/views/test/test2.vue");
const login = () => import(/* webpackChunkName: "login" */ "@/views/Login.vue");

export const constantRouterMap = [
  {
    path: "/login",
    name: "login",
    component: login,
  },
];

const routerList = [
  {
    path: "/:catchAll(.*)",
    redirect: "/login",
  },
  {
    path: "/dashboard",
    name: "dashboard",
    component: dashboard,
  },
  {
    path: "/test",
    name: "test",
    component: layout,
    redirect: "/test/test",
    children: [
      {
        path: "/test/test",
        name: "test1",
        component: testDom,
        meta: { title: "test1" },
      },
      {
        path: "/test/test2",
        name: "test2",
        component: test2,
        meta: { title: "test2" },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRouterMap.concat(routerList), // 这里做了区分 一个是固定的路由 一个是动态的路由
});

export default router;

在main.js中使用
const app = createApp(App);
...
import router from "@/router";
app.use(router); // 引用路由实例
在需要用到的页面使用 以及监听路由变化
<script setup>
  import { useRoute, useRouter } from 'vue-router'
  import { watch } from "vue";
  // 声明
  const route = useRoute()
  const router = useRouter()
 
  // 获取query
  console.log('route.query', route.query)
  // 获取params
  console.log('route.params', route.params)
 //监听路由变化
 watch(router, (to, from) => {
  		....
  });

  // 路由跳转
  router.push({
      path: '/index',
      query: {}
  })
</script>

实际项目开发下来 效率没有降低多少 相比之下 代码更加简洁了不少 算是有收获
不过
不知道是vue3的原因还是vite的原因 报错提醒不太完整 永远提示在 script标签的最后一行 这就很不好找问题了
而且遇到错误就页面空白了请求文件404 这个问题得排查好久 是一个大坑

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/601943.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

call, apply , bind 区别详解 及 实现购物车业务开发实例

call 方法&#xff1a; 原理 call 方法允许一个对象借用另一个对象的方法。通过 call&#xff0c;你可以指定某个函数运行时 this 指向的上下文。本质上&#xff0c;call 改变了函数运行时的作用域&#xff0c;它可以让我们借用一个已存 在的函数&#xff0c;而将函数体内的 th…

ISIS学习第一部分——isis基本概念

目录 一.ISIS与OSI模型 1.IS-IS&#xff0c;中间系统到中间系统 2.ES-IS,终端系统到中间系统 二.NET——ISIS中的“IP地址” &#xff08;1&#xff09;NET有3个部分: 1.Area ID 2.System ID 3.SEL &#xff08;2&#xff09;.前面是可变长的&#xff0c;如何进行区分…

前端开发攻略---使用Sass调整颜色亮度,实现Element组件库同款按钮

目录 1、演示 2、实现原理 3、实现代码 1、演示 2、实现原理 改变颜色亮度的原理是通过调整颜色的 RGB 值中的亮度部分来实现的。在 Sass 中&#xff0c;可以使用颜色函数来操作颜色的 RGB 值&#xff0c;从而实现亮度的调整。 具体来说&#xff0c;亮度调整函数通常会改变颜…

使用 Docker 部署 TaleBook 私人书籍管理系统

1&#xff09;项目介绍 GitHub&#xff1a;https://github.com/talebook/talebook Talebook 是一个简洁但强大的私人书籍管理系统。它基于 Calibre 项目构建&#xff0c;具备书籍管理、在线阅读与推送、用户管理、SSO 登录、从百度/豆瓣拉取书籍信息等功能。 友情提醒&#x…

ansible------inventory 主机清单

目录 inventory 中的变量 2&#xff09;组变量[webservers:vars] #表示为 webservers 组内所有主机定义变量&#xff0c;所有组内成 员都有效 ansible_userrootansible_passwordabc1234 3&#xff09; [all:vars…

前置知识储备

基本认知 什么是模式 在一定环境中解决一些问题的方案&#xff08;通俗来说&#xff1a;特定环境中用固定的套路解决问题&#xff09; 什么是设计模式 设计模式是一套反复被人使用&#xff0c;多数人知晓的&#xff0c;经过分类编目的代码设计经验的总结 设计模式最终的目…

[笔试训练](十五)

目录 043:平方数 044:分组 045:拓扑排序 043:平方数 平方数 (nowcoder.com) 题目&#xff1a; 题解&#xff1a; 简单题&#xff0c;开根号之后判断左右两个数哪个离得近。 #include <iostream> #include <cmath> using namespace std; typedef long long…

电脑文件批量重命名不求人:快速操作,高效技巧让你轻松搞定

在数字化时代&#xff0c;电脑文件的管理与整理显得尤为重要。当面对大量需要重命名的文件时&#xff0c;一个个手动修改不仅耗时&#xff0c;还容易出错。那么&#xff0c;有没有一种方法可以快速、高效地完成这一任务呢&#xff1f;答案是肯定的&#xff0c;下面就来介绍几种…

温暖家居新风尚,能率壁挂炉——设计新风尚,体验再升级

随着家居品质要求的提升&#xff0c;现代人对家居的舒适性和设计感有了更高的追求。壁挂炉&#xff0c;作为现代家居中不可或缺的一部分&#xff0c;其重要性日益凸显。中国国际供热通风空调、卫浴及舒适家居系统展览会&#xff08;ISH China & CIHE&#xff09;将于2024年…

测评工作室的养号成本,效率,纯净度,便捷性等问题怎么解决?

大家好&#xff0c;我是南哥聊跨境&#xff0c;最近有很多做测评工作室的朋友找到南哥&#xff0c;问我有什么新的测评养号系统可以解决成本&#xff0c;效率&#xff0c;纯净度&#xff0c;便捷性等问题 测评养号系统从最早的模拟器、虚拟机到911、VPS、手机设备等&#xff0…

【深度学习实战(33)】训练之model.train()和model.eval()

一、model.train()&#xff0c;model.eval()作用&#xff1f; model.train() 和 model.eval() 是 PyTorch 中的两个方法&#xff0c;用于设置模型的训练模式和评估模式。 model.train() 方法将模型设置为训练模式。在训练模式下&#xff0c;模型会启用 dropout 和 batch norm…

|Python新手小白中级教程|第二十三章:列表拓展之——元组

文章目录 前言一、列表复习1.索引、切片2.列表操作字符3.数据结构实践——字典 二、探索元组1.使用索引、切片2.使用__add__((添加元素&#xff0c;添加元素))3.输出元组4.使用转化法删除元组指定元素5.for循环遍历元组 三、元组VS列表1.区别2.元组&#xff08;tuple&#xff0…

零门槛副业兼职!10种长期赚钱好方法!

想要实现财务自由&#xff0c;不能仅停留在梦想层面&#xff0c;更需要付诸实践。 以下是我从网络上精心整理的十大可靠的兼职副业建议&#xff0c;旨在助你一臂之力。 这些项目已根据推荐程度、难度水平、目标人群以及预期收入进行了细致分类。 我要强调的是&#xff0c;任…

Cosmo Bunny Girl

可爱的宇宙兔女郎的3D模型。用额外的骨骼装配到Humanoid上,Apple混合了形状。完全模块化,包括不带衣服的身体。 技术细节 内置,包括URP和HDRP PDF。还包括关于如何启用URP和HDRP的说明。 LOD 0:面:40076,tris 76694,verts 44783 装配了Humanoid。添加到Humanoid中的其他…

带EXCEL附件邮件发送相关代码

1.查看生成的邮件 2.1 非面向对象的方式&#xff08;demo直接copy即可&#xff09; ​ REPORT Z12. DATA: IT_DOCUMENT_DATA TYPE SODOCCHGI1,IT_CONTENT_TEXT TYPE STANDARD TABLE OF SOLISTI1 WITH HEADER LINE,IT_PACKING_LIST TYPE TABLE OF SOPCKLSTI1 WITH HEADER LIN…

AI编码工具-通义灵码初识

AI编码工具-通义灵码初识 通义灵码支持环境及语言代码安全性 通义灵码安装通义灵码登录 关于通义灵码的初识&#xff0c;还是得从2023云栖大会来说起。2023云栖大会带来了跨越式升级的千亿级参数规模大模型——通义千问2.0&#xff0c;随之而来的便有通义灵码&#xff0c;那么什…

SpringBoot项目配置HTTPS接口的安全访问

参考&#xff1a;https://blog.csdn.net/weixin_45355769/article/details/131727935 安装好openssl后&#xff0c; 创建 D:\certificate CA文件夹下包含&#xff1a; index.txt OpenSSL在创建自签证书时会向该文件里写下索引database.txt OpenSSL会模拟数据库将一些敏感信息…

嵌入式移植7Z解压缩(纯C)

本文分享一个纯C语言编写的7Z解压缩代码库&#xff0c;本代码库的主要目的是在嵌入式环境下使用7z解压缩文件&#xff0c;可以将升级包通过7z进行压缩&#xff0c;然后发送给设备&#xff0c;减小和设备传输过程中的文件大小&#xff0c;进而达到传输大文件的目的。 下载链接 …

2024年最佳音频处理软件盘点!助你事半功倍

在数字媒体时代&#xff0c;音频处理软件已经成为音乐制作、音频编辑和后期处理不可或缺的工具。这些软件具备强大的功能&#xff0c;能帮助用户轻松实现声音的剪辑、混音、特效处理以及音频格式转换等操作。本文将为你介绍音频处理软件的基本概念、功能特点以及常用软件&#…

【@ohos.events.emitter (Emitter)】

ohos.events.emitter (Emitter) 本模块提供了在同一进程不同线程间&#xff0c;或同一进程同一线程内&#xff0c;发送和处理事件的能力&#xff0c;包括持续订阅事件、单次订阅事件、取消订阅事件&#xff0c;以及发送事件到事件队列的能力。 说明&#xff1a; 本模块首批接…
最新文章