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,
})
],
})
import { defineComponent, ref } from "vue"
import "./App.scss"
const App = defineComponent({
setup() {
const count = ref(0)
const onClick = () => {
count.value++
}
return () => (
<>
{count.value}
>
)
},
})
export default App
任何以
.module.css
为后缀名的 CSS 文件都被认为是一个 CSS modules 文件
/* example.module.css */
.red {
color: red;
}
import classes from './example.module.css'
document.getElementById('foo').className = classes.red
Chrome 远程调试手机页面
http://192.168.3.37:3000
访问我电脑上的网页应用场景: 可以对其中路由做动画
// 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 () =>
xx记账
// 使用对应的 name
}
})
import { computed, onMounted, onUnmounted, ref, Ref } from "vue"
type Point = { x: number; y: number }
export const useSwipe = (element: Ref) => {
const start = ref(null)
const end = ref(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 使用
import { defineComponent } from "vue"
import s from "./button.module.scss"
interface ButtonProps {
onClick: (e: MouseEvent) => void
}
export const Button = defineComponent({
inheritAttrs: false, // 不让 vue 帮我自动继承属性
// 插槽 context.slots
setup: (props, context) => {
return () =>
},
})
// 具名插槽
import { defineComponent, PropType } from "vue"
import s from "NavBar.module.scss"
export const NavBar = defineComponent({
setup: (props, context) => {
const { slots } = context
return () => (
)
},
})
// 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,
required: true,
}
},
setup: (props) => {
const { name } = props
return () => (
)
},
})
'\u{1F600}'
// 创建响应式数据
const formData = reactive({
name: '',
sign: ''
})
const onSubmit = (e: Event) => {
e.preventDefault()
console.log("proxy", formData) // proxy
console.log("proxy", toRaw(formData)) // json
}
// v-model 使用