【web系列八】vscode下vue3+vue/cli+pinia+elementplus+typescript搭建前端开发环境

需要准备的包

vscode
https://code.visualstudio.com/#hundreds-of-extensions
node.js
http://nodejs.cn/download/

配置环境

node.js

直接使用下载的安装包安装,node.js附带了npm,npm类似于python中的pip

vue-cli

全局安装

npm install -g @vue/cli

查看版本

vue -V

pinia

npm install -s pinia

element-plus

npm install element-plus --save
# 创建项目
```bash
vue create test

配置选项如下

在这里插入图片描述
勾选上typescript
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

导入pinia

将main.ts改成如下形式:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'

createApp(App).use(router).use(createPinia()).mount('#app')

导入element-plus

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

createApp(App).use(router).use(ElementPlus).use(createPinia()).mount('#app')

启动项目

cd test

开发者模式

npm run serve

生产模式

npm run build

使用Pinia+添加一个vue组件

创建商店

在src下新建一个store文件夹,并在store中新建一个index.js文件

import { defineStore } from 'pinia'

export const mainStore = defineStore('main', {
	state: ()=>{
		return {
			hello: 'Hello Pinia!'
		}
	},
	getters: {},
	actions: {}
})

创建新组件

在components文件夹下创建一个Hello.vue

<template>
	<div id='app'>
		<h3>{{ store.hello }}</h3>
		<h3>解构:{{ hello }}</h3>
	</div>
</template>

<script setup>
import { mainStore } from '../store'
import { storeToRefs } from 'pinia'

const store = mainStore()
const { hello } = storeToRefs(store)
</script>

<style></style>

部署组件

咱们就给他部署在主页上,修改一下HomeView.vue

<template>
	<div class='home'>
		<img = alt='Vue logo' src='../assets/logo.png'>
		<HelloWorld msg='Welcome to Your Vue.js + TypeScript App'/>
		<Hello></Hello>
	</div>
</template>

<script lang='ts'>
import { Options, Vue } from 'vue-class-component';
import HelloWorld from '@/components/HelloWorld.vue';
import Hello from '@/components/Hello.vue';

@Options({
	components: {
		HelloWorld,
		Hello
	},
})
export default class HomeView extends Vue {}
</script>

关闭格式检测

这时如果直接运行的话,会报错
Component name “Hello” should always be multi-word
这是由于Vue的ESLINT检查,具体原因如下:
https://blog.csdn.net/u013078755/article/details/123581070
咱们只要修改一下vue.config.js

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
	transpileDependencies: true,
	lintOnSave: false //关闭eslint检查
})

这时再启动工程就可以看到我们添加的组件啦
在这里插入图片描述

参考资料

https://blog.csdn.net/u013078755/article/details/123581070

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一种流行的JavaScript框架,它提供了用于构建用户界面的高效工具和组件。在使用Vue3时,结合TypeScript可以提供更好的类型检查和开发体验。Vue Router是Vue框架的官方路由管理工具,能够轻松实现单页面应用的路由功能。 PiniaVue3官方推荐的状态管理工具。它使用类似于Vuex的store模式,但通过使用Vue3的响应性机制,提供了更好的性能和开发体验。 Element Plus是一套基于Vue3的UI组件库,它提供了丰富的功能和美观的界面,可以用于构建现代化的Web应用程序。 在搭建Vue3和TypeScript环境模板时,我们可以使用Vite这个工具。Vite是一个快速的Web开发工具,它基于ES模块的原生浏览器导入功能,能够提供快速的冷启动和热模块替换。我们可以使用Vite的模板来创建一个基于Vue3和TypeScript的项目骨架,以及集成Vue Router和Element Plus。 在进行Vite打包优化时,我们可以采取以下措施来提高打包效率和性能: 1. 使用按需加载:只加载项目所需的模块,减少打包体积和加载时间。 2. 配置代码分割:将代码拆分为多个小块,按需加载,提高页面加载速度。 3. 使用压缩和混淆工具:使用压缩和混淆工具来减小打包体积,提高加载速度。 4. 配置缓存策略:启用浏览器缓存,减少重复加载和网络请求。 5. 使用CDN加速:将一些公共资源,如Vue、Element Plus等引入CDN,提高加载速度。 6. 优化依赖管理:检查项目的依赖关系,合理管理和更新依赖,避免冗余和重复引入。 通过以上优化措施,我们可以提高项目的打包效率和性能,提供更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值