当前位置:   article > 正文

vue3的自动引入ref,computed,reactive等的插件 unplugin-auto-import_vue3自动引入ref

vue3自动引入ref

1、安装插件

npm install -D unplugin-auto-import 
  • 1

2、在vite-config.ts中的配置

//先引入
import AutoImport from "unplugin-auto-import/vite"
export default defineConfig({
  plugins: [
      vue(),
      //然后配置
      AutoImport({
       imports: ['vue'],
        dts: 'src/auto-import.d.ts'
     }),
  ]
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3、引入之前我们在每个组件中都得写入如下:
import { reactive, computed, onMounted, ref, toRefs } from “vue”;

<template>
	<div class="login_main">
		<div class="login_content">
			<h2>账号登录</h2>
			<a-form autocomplete="off">
				<a-form-item class="login_form">
					<a-input placeholder="请输入账号" maxLength="11" v-model:value="username" />
				</a-form-item>
				<a-form-item class="login_form login_password">
					<a-input-password maxLength="11" placeholder="请输入密码" v-model:value="password" />
				</a-form-item>
				<a-form-item class="login_form login_verify">
					<a-input placeholder="请输入验证码" maxLength="6" v-model:value="verify" />
					<img @click="changeVerify" :src="imgurl" alt="" />
				</a-form-item>
				<a-form-item class="login_form">
					<a-button type="primary" :size="size" @click="handleClick">登录</a-button>
				</a-form-item>
			</a-form>
		</div>
	</div>
</template>

<script lang="ts">
//引入前我们都得写如下import 
import { reactive, computed, onMounted, ref, toRefs } from "vue";
import type { SizeType } from "ant-design-vue/es/config-provider";
// 引入公共js文件
import utils from "@/utils/tools";
import { getCapture, loginApi } from "@/api/login";
interface FormState {
	username: string;
	password: string;
	verify: string;
}
export default {
	setup() {
		const formState: FormState = reactive({
			username: "",
			password: "",
			verify: "",
		});
		const size = ref<SizeType>("large");
		const verityCode = utils.getCookie("appletSystem:sess");
		console.log(verityCode);
		const formDataObj = reactive({
			imgurl: "",
		});
		const changeVerify = (): void => {
			getCapture().then((res) => {
				formDataObj.imgurl = res.data.imgurl;
			});
		};
		changeVerify();
		const handleClick = () => {
			loginApi().then((res) => {
				console.log(res);
			});
		};
		return {
			...toRefs(formState),
			size,
			changeVerify,
			...toRefs(formDataObj),
			handleClick,
		};
	}
}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

4、引入unplugin-auto-import插件后

<template>
	<div class="login_main">
		<div class="login_content">
			<h2>账号登录</h2>
			<a-form autocomplete="off">
				<a-form-item class="login_form">
					<a-input placeholder="请输入账号" maxLength="11" v-model:value="username" />
				</a-form-item>
				<a-form-item class="login_form login_password">
					<a-input-password maxLength="11" placeholder="请输入密码" v-model:value="password" />
				</a-form-item>
				<a-form-item class="login_form login_verify">
					<a-input placeholder="请输入验证码" maxLength="6" v-model:value="verify" />
					<img @click="changeVerify" :src="imgurl" alt="" />
				</a-form-item>
				<a-form-item class="login_form">
					<a-button type="primary" :size="size" @click="handleClick">登录</a-button>
				</a-form-item>
			</a-form>
		</div>
	</div>
</template>

<script lang="ts">
import type { SizeType } from "ant-design-vue/es/config-provider";
// 引入公共js文件
import utils from "@/utils/tools";
import { getCapture, loginApi } from "@/api/login";
interface FormState {
	username: string;
	password: string;
	verify: string;
}
export default {
	setup() {
		const formState: FormState = reactive({
			username: "",
			password: "",
			verify: "",
		});
		const size = ref<SizeType>("large");
		const verityCode = utils.getCookie("appletSystem:sess");
		console.log(verityCode);
		const formDataObj = reactive({
			imgurl: "",
		});
		const changeVerify = (): void => {
			getCapture().then((res) => {
				formDataObj.imgurl = res.data.imgurl;
			});
		};
		changeVerify();
		const handleClick = () => {
			loginApi().then((res) => {
				console.log(res);
			});
		};
		return {
			...toRefs(formState),
			size,
			changeVerify,
			...toRefs(formDataObj),
			handleClick,
		};
	}
}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

这样是不是方便许多了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/213591
推荐阅读
相关标签
  

闽ICP备14008679号