Vue3简单使用(一) --- 环境搭建

node版本管理工具nvm,nvm list、nvm use 14.18.0

可以简单启动服务器:npx serve

安装包:npm install xx1 xx2,npm install -D xx3 xx4

vue提供了多个版本

传统项目引入

全局构建版本
html 复制代码
<div id="app">
  <button @click="count++">
    Count is: {{ count }}
  </button>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script type="module">
const { createApp, ref } = Vue
createApp({
  setup() {
    return {
      count: ref(0)
    }
  }
}).mount('#app')
</script>
ES模块构建版本
html 复制代码
<div id="app">{{ message }}</div>
<!-- 使用importmap可以import from 字符串 -->
<script type="importmap">
  {
    "imports": {
      "vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
    }
  }
</script>
<script type="module">
  // import { createApp, ref } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js'
  import { createApp, ref } from 'vue'
  createApp({
    setup() {
      const message = ref('Hello Vue!')
      return {
        message
      }
    }
  }).mount('#app')
</script>
拆分模块

在传统项目中因为无法使用vite、webpack对vue进行编译,只能使用js拆分模块

javascript 复制代码
<!-- index.html -->
<div id="app"></div>
<script type="module">
  import { createApp } from 'vue'
  import MyComponent from './my-component.js'
  createApp(MyComponent).mount('#app')
</script>

// my-component.js
import { ref } from 'vue'
export default {
  setup() {
    const count = ref(0)
    return { count }
  },
  template: `<div>count is {{ count }}</div>`
}

微服务项目

脚手架

可以简单使用官方提供脚手架创建可用项目。

bash 复制代码
npm create vue@latest
空项目开始

1.vue、vite、@vitejs/plugin-vue 三个包是必须的

bash 复制代码
npm install vue
npm install -D vite @vitejs/plugin-vue
  1. 修改package.json,增加npm脚本(scripts)
javascript 复制代码
{
  "scripts": {
    "dev": "vite"
  },
  "dependencies": {
    "vue": "^3.3.8"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.5.0",
    "vite": "^5.0.0"
  }
}
  1. 增加vite.config.js,引入vite、@vitejs/plugin-vue插件
javascript 复制代码
import vue from '@vitejs/plugin-vue'
import {defineConfig} from 'vite'

export default defineConfig({
	plugins:[
		vue()
	]
})

4.至此,环境基本完成,可以继续添加 index.html、main.js、com.vue等页面

html 复制代码
<!-- index.html -->
<div id="app"/>

<script type="module">
import {createApp} from 'vue'
import com1 from './com1.vue'

let app = createApp(com1)
app.mount('#app')
</script>

<!-- com1.vue -->
<template>
<div>
	com1
</div>
</template>
相关推荐
haorooms6 分钟前
Promise.try () 完全指南
前端·javascript
kyriewen7 分钟前
闭包:那个“赖着不走”的家伙,到底有什么用?
前端·javascript·ecmascript 6
斌味代码10 分钟前
el-popover跳转页面不隐藏,el-popover销毁
前端·javascript·vue.js
嫂子的姐夫21 分钟前
040-spiderbuf第C8题
javascript·爬虫·python·js逆向·逆向
哈__1 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-device-info
javascript·react native·react.js
庄小焱1 小时前
React——React基础语法(2)
前端·javascript·react.js
终端鹿1 小时前
Vue3 核心 API 深度解析:ref / reactive / computed / watch
前端·javascript·vue.js
console.log('npc')1 小时前
partial在react接口定义中是什么意思
前端·javascript·typescript
SuperEugene1 小时前
前端 utils 工具函数规范:拆分 / 命名 / 复用全指南,避开全局污染等高频坑|编码语法规范篇
开发语言·前端·javascript
Amumu121382 小时前
Js:内置对象
开发语言·前端·javascript