总结vue3 的一些知识点

简介: vue3 支持 jsx

vue3 支持 jsx


安装依赖

pnpm add @vitejs/plugin-vue-jsx
vite.config.ts 中引用插件
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx({
      transformOn: true,
      mergeProps: true,
    })
  ],
})


使用 jsx

import { defineComponent, ref } from "vue"
import "./App.scss"
const App = defineComponent({
  setup() {
    const count = ref(0)
    const onClick = () => {
      count.value++
    }
    return () => (
      <>
        <div class='page'>
          {count.value}
        </div>
        <button onClick={onClick}>增加</button>
      </>
    )
  },
})
export default App



CSS Module

任何以 .module.css 为后缀名的 CSS 文件都被认为是一个 CSS modules 文件

/* example.module.css */
.red {
  color: red;
}
import classes from './example.module.css'
document.getElementById('foo').className = classes.red


手机调试 H5


Chrome 远程调试手机页面


用 usb 连接手机和电脑

手机开启 usb 调试

手机打开要调试的页面 (ip地址 => http://192.x.x.1:端口号/)

手机浏览器输入 http://192.168.3.37:3000 访问我电脑上的网页

打开

edge://inspect

https://links.jianshu.com/go?to=edge%3A%2F%2Finspect

或者

chrome://inspect/#devices

https://links.jianshu.com/go?to=chrome%3A%2F%2Finspect%2F%23devices

页面, 等待一会, 找到对应页面


vue-router 一个路由多个视图


应用场景: 可以对其中路由做动画

// routes.ts
export const routes: RouteRecordRaw[] = [
  { path: "/", redirect: "/welcome" },
  {
    path: "/welcome",
    component: Welcome,
    children: [
      { path: "", redirect: "/welcome/one" },
      // components 可以用对象的方式
      { path: "one", components: { main: First, footer: FirstActions } },
      { path: "two", components: { main: Second, footer: SecondActions } },
      { path: "three", components: { main: Third, footer: ThirdActions } },
      { path: "four", components: { main: Forth, footer: ForthActions } },
    ],
  }
]


// 视图使用
import { defineComponent, h, Transition, VNode } from 'vue';
import { RouteLocationNormalizedLoaded, RouterView } from 'vue-router';
import s from './Welcome.module.scss'
export const Welcome = defineComponent({
  setup: (props, context) => {
    return () => <div class={s.wrapper}>
      <header>
        <h1>xx记账</h1>
      </header>
      <main class={s.main}>
        // 使用对应的 name
        <RouterView name="main">
        </RouterView>
      </main>
      <footer>
        <RouterView name="footer" />
      </footer>
    </div>
  }
})



SvgSprite svg 雪碧图


useSwipe 滑动 hooks


import { computed, onMounted, onUnmounted, ref, Ref } from "vue"
type Point = { x: number; y: number }
export const useSwipe = (element: Ref<HTMLElement | null>) => {
  const start = ref<Point | null>(null)
  const end = ref<Point | null>(null)
  const swiping = ref(false)
  // 可以设置距离多少再移动
  const distance = computed(() => {
    if (!end.value || !start.value) return null
    return {
      x: end.value?.x - start.value?.x,
      y: end.value?.y - start.value?.y,
    }
  })
  const direction = computed(() => {
    if (!swiping) return null
    if (!distance.value) return null
    const { x, y } = distance.value
    if (Math.abs(x) > Math.abs(y)) {
      return x > 0 ? "right" : "left"
    } else {
      return y > 0 ? "down" : "up"
    }
  })
  const onStart = (e: TouchEvent) => {
    const { clientX, clientY } = e.touches[0]
    start.value = {
      x: clientX,
      y: clientY,
    }
    end.value = null
    swiping.value = true
  }
  const onMove = (e: TouchEvent) => {
    const { clientX, clientY } = e.touches[0]
    if (swiping.value) {
      end.value = {
        x: clientX,
        y: clientY,
      }
    }
  }
  const onEnd = (e: TouchEvent) => {
    swiping.value = false
    start.value = null
    end.value = null
  }
  onMounted(() => {
    if (!element.value) return null
    element.value.addEventListener("touchstart", onStart)
    element.value.addEventListener("touchmove", onMove)
    element.value.addEventListener("touchend", onEnd)
  })
  onUnmounted(() => {
    if (!element.value) return null
    element.value.removeEventListener("touchstart", onStart)
    element.value.removeEventListener("touchmove", onMove)
    element.value.removeEventListener("touchend", onEnd)
  })
  return {
    swiping,
    distance,
    direction,
  }
}



组件使用插槽(slots) 和 属性 props

// slots 使用
import { defineComponent } from "vue"
import s from "./button.module.scss"
interface ButtonProps {
  onClick: (e: MouseEvent) => void
}
export const Button = defineComponent<ButtonProps>({
  inheritAttrs: false, // 不让 vue 帮我自动继承属性
  // 插槽 context.slots
  setup: (props, context) => {
    return () => <button class={s.button}>{context.slots.default?.()}</button>
  },
})
// 具名插槽
import { defineComponent, PropType } from "vue"
import s from "NavBar.module.scss"
export const NavBar = defineComponent({
  setup: (props, context) => {
    const { slots } = context
    return () => (
      <div class={s.navbar}>
        <span class={s.icon_wrapper}>{slots.icon?.()}</span>
        <span class={s.title_wrapper}>{slots.default?.()}</span>
      </div>
    )
  },
})



// props 使用

import { defineComponent, PropType } from "vue"
import s from "./Icon.module.scss"
export type IconNames = "add" | "chart" | "clock" | "cloud"
export const Icon = defineComponent({
  // inheritAttrs: false,
  props: {
    name: {
      type: String as PropType<IconNames>,
      required: true,
    }
  },
  setup: (props) => {
    const { name } = props
    return () => (
      <svg class={s.icon}>
        <use xlinkHref={"#" + name}></use>
      </svg>
    )
  },
})


表情 搜索 emoji full list

[表情](

Full Emoji List, v15.0 (unicode.org)

https://links.jianshu.com/go?to=https%3A%2F%2Funicode.org%2Femoji%2Fcharts%2Ffull-emoji-list.html

)

'\u{1F600}'


reactive & toRaw

// 创建响应式数据
const formData = reactive({
  name: '',
  sign: ''
})
const onSubmit = (e: Event) => {
  e.preventDefault()
  console.log("proxy", formData) // proxy
  console.log("proxy", toRaw(formData)) // json
}
// v-model 使用
<input v-model={formData.name} class={[s.formItem, s.input, s.error]}></input>


相关文章
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
1578 139
|
缓存 JavaScript PHP
斩获开发者口碑!SnowAdmin:基于 Vue3 的高颜值后台管理系统,3 步极速上手!
SnowAdmin 是一款基于 Vue3/TypeScript/Arco Design 的开源后台管理框架,以“清新优雅、开箱即用”为核心设计理念。提供角色权限精细化管理、多主题与暗黑模式切换、动态路由与页面缓存等功能,支持代码规范自动化校验及丰富组件库。通过模块化设计与前沿技术栈(Vite5/Pinia),显著提升开发效率,适合团队协作与长期维护。项目地址:[GitHub](https://github.com/WANG-Fan0912/SnowAdmin)。
1670 5
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
827 1
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
1439 11
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
911 0
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
1416 1
|
JavaScript API 容器
Vue 3 中的 nextTick 使用详解与实战案例
Vue 3 中的 nextTick 使用详解与实战案例 在 Vue 3 的日常开发中,我们经常需要在数据变化后等待 DOM 更新完成再执行某些操作。此时,nextTick 就成了一个不可或缺的工具。本文将介绍 nextTick 的基本用法,并通过三个实战案例,展示它在表单验证、弹窗动画、自动聚焦等场景中的实际应用。
1438 17
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
779 0
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
534 0
|
JavaScript 前端开发 算法
Vue 3 和 Vue 2 的区别及优点
Vue 3 和 Vue 2 的区别及优点
1079 12