All Projects → weizhanzhan → Vue3 Ts Template H5

weizhanzhan / Vue3 Ts Template H5

🍰🤡 Vue3.0 + Typescript + Sass + Vant 移动端 ,附属微信朋友圈demo

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Vue3 Ts Template H5

Wxdraw
A lightweight canvas library which providing 2d draw for weapp 微信小程序2d动画库 😎 🐼
Stars: ✭ 1,625 (+1628.72%)
Mutual labels:  html5, webapp
Saltgui
A web interface for managing SaltStack based infrastructure.
Stars: ✭ 278 (+195.74%)
Mutual labels:  html5, webapp
React Template Easily
简单易用的react工程化模板,适用于h5,webapp,hybrid开发
Stars: ✭ 151 (+60.64%)
Mutual labels:  html5, webapp
Scalc
📲 A simple calculator application
Stars: ✭ 336 (+257.45%)
Mutual labels:  html5, webapp
Spellbook Of Modern Webdev
A Big Picture, Thesaurus, and Taxonomy of Modern JavaScript Web Development
Stars: ✭ 13,450 (+14208.51%)
Mutual labels:  html5, webapp
Theorytracker
🎼 HTML5/WebAudio multi-track functional harmony analysis and songwriting app! -- https://hlorenzi.github.io/theorytracker/
Stars: ✭ 62 (-34.04%)
Mutual labels:  html5, webapp
Springbootangularhtml5
♨️ Spring Boot 2 + Angular 11 + HTML5 router mode + HTTP interceptor + Lazy loaded modules
Stars: ✭ 89 (-5.32%)
Mutual labels:  html5
Phasereditor2d V3
A web-based IDE for HTML5 game development. Powered by Phaser.
Stars: ✭ 92 (-2.13%)
Mutual labels:  html5
React Froala Design Blocks
React implementation for Froala Design Blocks.
Stars: ✭ 89 (-5.32%)
Mutual labels:  html5
Supermarket
设计精良的网上商城系统,包括前端、后端、数据库、负载均衡、数据库缓存、分库分表、读写分离、全文检索、消息队列等,使用SpringCloud框架,基于Java开发。该项目可部署到服务器上,不断完善中……
Stars: ✭ 1,278 (+1259.57%)
Mutual labels:  html5
Is Engine
SFML C++ game engine that allows to create games on Web (HTML 5 - CSS 3), Android and PC
Stars: ✭ 94 (+0%)
Mutual labels:  html5
Fingermemory
以刻意练习理论为指导、根据练习者掌握情况动态生成训练内容,优先训练练习者掌握不佳内容的五笔输入法训练系统。
Stars: ✭ 93 (-1.06%)
Mutual labels:  html5
Personalworks
个人作品
Stars: ✭ 91 (-3.19%)
Mutual labels:  webapp
Snapaper
📰 Past Papers Sharing Platform Based On Vue.js & GCE Guide | CAIE 试卷分享与下载平台
Stars: ✭ 90 (-4.26%)
Mutual labels:  html5
Iver
ui component for vue2.0
Stars: ✭ 92 (-2.13%)
Mutual labels:  webapp
Buddycss
The framework for people who love coding!
Stars: ✭ 89 (-5.32%)
Mutual labels:  html5
My Animate
使用 HTML5 / Canvas / CSS3 制作各种动画效果
Stars: ✭ 93 (-1.06%)
Mutual labels:  html5
Vkbot
Простой разговорный бот на PHP
Stars: ✭ 88 (-6.38%)
Mutual labels:  html5
Front End Course
前端知识学习汇总
Stars: ✭ 91 (-3.19%)
Mutual labels:  html5
Tutorialdb
A search 🔎 engine for programming/dev tutorials, See it in action 👉
Stars: ✭ 93 (-1.06%)
Mutual labels:  webapp

vue3-ts-template-h5

vue3

Vue3.0 + Typescript 初体验,打造h5,webapp移动端模板,开箱即用

📖 Vue3.0 相关文档:

相关库名称 在线地址 🔗
Vue3.0 官方文档 链接
Vue3.0 中文文档 链接 国内链接
Composition-API手册 链接
Vue3.0 源码学习 链接
Vue-Router 官方文档 链接
Vuex4.0(目前在 beta 阶段) Github

demo浏览

目录

基础搭建

  • vue3配置
# 1.安装vue-cli next
npm install --global @vue/[email protected]

# 2.创建项目,创建选择模板的时候,选择“Manually select features",下面有我的options,仅供参考
vue create my-project-name

# 如果已经有了一个cli项目不是TypeScript,可以增加一个cli的插件
vue add typescript

My Vue CLI Option

Vue CLI v4.5.4

  1. Please pick a preset: Manually select features
  2. Check the features needed for your project: Choose Vue version, Babel, TS, Router, Vuex, CSS Pre-processors, Linter
  3. Choose a version of Vue.js that you want to start the project with 3.x (Preview)
  4. Use class-style component syntax? Yes
  5. Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes
  6. Use history mode for router? (Requires proper server setup for index fallback in production) Yes
  7. Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with node-sass)
  8. Pick a linter / formatter config: Prettier
  9. Pick additional lint features: Lint on save
  10. Where do you prefer placing config for Babel, ESLint, etc.? (Use arrow keys) In dedicated config files

vue3的新特性和改动

1.v-model

2.x语法

在 2.x 中,在组件上使用 v-model 相当于绑定 value prop 和 input 事件:

<child-component v-model="title" />
<!-- 语法糖 默认mode prop:value  event:input-->
<child-component :value="title" @input="title = $event"/>

使用v-bind:sync vue是单向数据流,为了对prop进行"双向绑定",可以是用sync来实现

<child-component :title.sync="title" />
<!-- 语法糖 -->
<child-component :title="title" @update:title="title = $event"/>

子组件内通过下面方式通知父父组件

this.$emit('update:title',value)

3.x语法

在3.x中自定义组件的v-model,是传递了modelValueprop并接受抛出的update:modelValue事件,和sync很像

<child-component v-model="title" />
<!-- 语法糖 -->
<child-component :modelValue="title" @update:modelValue="title = $event"/>

v-model参数 若需要更改 model 名称,而不是更改组件内的 model 选项,而是将一个 argument 传递给 model

<child-component v-model:title="pageTitle" />
<!-- 简写: -->
<child-component :title="title" @update:title="title = $event" />

因此我们直接可以2.x的sync改成现在这种写法

<child-component :title.sync="title" />
<!-- 替换为 -->
<child-component v-model:title="title" />

并且一个子组件我们可以写多个v-model

<child-component v-model:title="pageTitle" v-model:content="content"/>

v-for

v-for的变动最主要体现在key上面,我们在使用<template v-for>的时候,2.x语法的key值不可以加在template标签上面,要加到子节点上,而在3.x中则可以加到template上,并且不需要在子节点上添加key

<!-- Vue 2.x -->
<template v-for="item in list">
  <div :key="item.id">...</div>
  <span :key="item.id">...</span>
</template>

<!-- Vue 3.x -->
<template v-for="item in list" :key="item.id">
  <div>...</div>
  <span>...</span>
</template>

ref

在2.0中,在v-for中绑定ref,我们通过$ref,获取的是一个ref数组,在3.x中则不会自动创建数组,我们需要绑定一个函数,自己处理并接受它

<div v-for="item in list" :ref="setItemRef"></div>
export default {
  setup() {
    //itemRefs 不必是数组:它也可以是一个对象,其 ref 会通过迭代的 key 被设置。
    let itemRefs = []
    const setItemRef = el => {
      itemRefs.push(el)
    }
    return {
      itemRefs,
      setItemRef
    }
  }
}

整理中...(敬请期待😄)

CompositionApi

VUE 3 COMPOSITION API CHEAT SHEET

<template>
  <div>
    <p>Spaces Left: {{ spacesLeft }} out of {{ capacity }}</p>
    <h2>Attending</h2>
    <ul>
      <li v-for="(name, index) in attending" :key="index">
        {{ name }}
      </li>
    </ul>
    <button @click="increaseCapacity()">Increase Capacity</button>
  </div>
</template> <script>
// If using Vue 2 with Composition API plugin configured/ 在Vue2中使用 Composition API :  import { ref, computed } from "@vue/composition-api";
import { ref, computed } from "vue";
export default {
  setup() {
    //数据响应式 将数据包装在对象中以跟踪更改
    const capacity = ref(4);
    const attending = ref(["Tim", "Bob", "Joe"]);
    //Computed属性
    const spacesLeft = computed(() => {
      //通过调用.value访问响应式引用的值
      return capacity.value - attending.value.length;
    });
    // 定义方法
    function increaseCapacity() {
      //ref进行响应式的变量 需要修改变量的话则需要对其.value操作
      capacity.value++;
    }
    // 使我们的模板可以访问这些对象和功能
    return { capacity, attending, spacesLeft, increaseCapacity };
  }
};
</script>

你也可以这样写

import { reactive, computed, toRefs } from "vue";
export default {
  setup() {
    //reactive接受一个对象并返回一个响应式对象
    const event = reactive({
      capacity: 4,
      attending: ["Tim", "Bob", "Joe"],
      spacesLeft: computed(() => { return event.capacity - event.attending.length; })
    });
    function increaseCapacity() {
      // reactive返回的响应式对象不需要使用.value操作
      event.capacity++;
    }
    //...toRefs 解构event中的对象,使模板中直接可以使用capacity或者attending,不需要event.attending
    return { ...toRefs(event), increaseCapacity };
  }
};

整理中...(敬请期待😄)

其他

getCurrentInstance

在setup中,是没有办法通过this获取到vue,我们可以通过getCurrentInstance获取vue实例

Vant配置

  • 安装
# 通过 npm 安装
npm i [email protected] -S

# 通过 yarn 安装
yarn add [email protected]
  • 使用 ts-import-plugin 实现vant按需引入 如果本地找不到这个这两个包就分别安装
//vue.config.js

// eslint-disable-next-line @typescript-eslint/no-var-requires
const merge = require("webpack-merge");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsImportPluginFactory = require("ts-import-plugin");


module.exports = {
  chainWebpack: config => {
    config.module
      .rule("ts")
      .use("ts-loader")
      .tap(options => {
        options = merge(options, {
          transpileOnly: true,
          getCustomTransformers: () => ({
            before: [
              tsImportPluginFactory({
                libraryName: "vant",
                libraryDirectory: "es",
                style: true
              })
            ]
          }),
          compilerOptions: {
            module: "es2015"
          }
        });
        return options;
      });
  }
};

  • 移动端适配(vw/vh方案)
# 安装依赖
npm install postcss-px-to-viewport -D
// vue.config.js
const pxtoviewport = require("postcss-px-to-viewport");

const autoprefixer = require("autoprefixer");

module.exports = {
  css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer(),
          pxtoviewport({
            viewportWidth: 375, // 视窗的宽度,对应的是我们设计稿的宽度,一般是750
            minPixelValue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
            unitPrecision: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
          })
        ]
      }
    }
  }
}

重新运行,px变vw,ok✌~~

  • 注册使用的vant全局组件,统一管理,避免重复引用
// plugins/vant.ts

import { App as VM } from "vue";
import { Button, List, Cell, Tabbar, TabbarItem } from "vant";

const plugins = [Button, List, Cell, Tabbar, TabbarItem];

export const vantPlugins = {
  install: function(vm: VM) {
    plugins.forEach(item => {
      vm.component(item.name, item);
    });
  }
};

//main.ts 使用

import { createApp } from 'vue'
import { vantPlugins } from './plugins/vant'

createApp(App)
  ...//其他配置
  .use(vantPlugins)
  .mount('#app')

vant主题色

//详见/src/theme/var.less
// Color Palette
@black: #000;
@white: #fff;
@gray-1: #f7f8fa;
@gray-2: #f2f3f5;
@gray-3: #ebedf0;
@gray-4: #dcdee0;
@gray-5: #c8c9cc;
@gray-6: #969799;
@gray-7: #646566;
@gray-8: #323233;
@red: #ee0a24;
@blue: #1989fa;
@orange: #ff976a;
@orange-dark: #ed6a0c;
@orange-light: #fffbe8;
@green: #07c160;
@green1:#4fc08d;
// Gradient Colors
@gradient-red: linear-gradient(to right, #ff6034, #ee0a24);
@gradient-orange: linear-gradient(to right, #ffd01e, #ff8917);

// Component C
  • 1.引入样式文件 新增上述文件,并引入,由于上面vant配置中已经引入了,我们要调整一下指定样式的路径
//vue.config.js
module.exports = {
   chainWebpack: config => {
    config.module
      .rule("ts")
      .use("ts-loader")
      .tap(options => {
        options = merge(options, {
          transpileOnly: true,
          getCustomTransformers: () => ({
            before: [
              tsImportPluginFactory({
                libraryName: "vant",
                libraryDirectory: "es",
                // --> 指定样式的路径
                style: name => `${name}/style/less`
              })
            ]
          }),
          compilerOptions: {
            module: "es2015"
          }
        });
        return options;
      });
  }
};

  • 2.修改样式变量
//vue.config.js
module.exports = {
  ...
  css: {
    loaderOptions: {
      //配置less主题
      less: {
        lessOptions: {
          modifyVars: {
            // 直接覆盖变量
            "text-color": "#111",
            "border-color": "#eee",
            // 或者可以通过 less 文件覆盖(文件路径为绝对路径)
            hack: `true; @import "./src/theme/var.less";`
          }
        }
      },
    }
  }
}


浏览器样式重置

重置浏览器标签的样式表,因为浏览器的品种很多,每个浏览器的默认样式也是不同的,比如button标签,在IE浏览器、Firefox浏览器以及Safari浏览器中的样式都是不同的,所以,通过重置button标签的CSS属性,然后再将它统一定义,就可以产生相同的显示效果。开始一个项目前,先创建一个reset.css,可以规避很多浏览器差异问题

/* http://meyerweb.com/eric/tools/css/reset/
   v5.0.1 | 20191019
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, menu, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
main, menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, main, menu, nav, section {
	display: block;
}
/* HTML5 hidden-attribute fix for newer browsers */
*[hidden] {
    display: none;
}
body {
	line-height: 1;
}
menu, ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}

移动端1px边框

  • 问题分析:有些手机的屏幕分辨率较高,是2-3倍屏幕。css样式中border:1px solid red;在2倍屏下,显示的并不是1个物理像素,而是2个物理像素。解决方案如下:
  • 利用 css 的 伪元素::after + transfrom 进行缩放 为什么用伪元素? 因为伪元素::after或::before是独立于当前元素,可以单独对其缩放而不影响元素本身的缩放

伪元素大多数浏览器默认单引号也可以使用,和伪类一样形式,而且单引号兼容性(ie)更好些 我是用scss写的mixins,其他与编译器道理道理都差不多

/*单条border样式*/
@mixin border-1px ($color, $direction) {
  position: relative;
  border: none;
  &::after{
    content: '';
    position: absolute;
    background: $color;
    @if $direction == left {
      left: 0;
      top: 0;
      height: 100%;
      width: 2px;
      transform: scaleX(0.5);
      transform-origin: left 0;
    }
    @if $direction == right {
      right: 0;
      top: 0;
      height: 100%;
      width: 2px;
      transform: scaleX(0.5);
      transform-origin: right 0;
    }
    @if $direction == bottom {
      bottom: 0;
      left: 0;
      width: 100%;
      height: 2px;
      transform: scaleY(0.5);
      transform-origin: 0 bottom;
    }
    @if $direction == top {
      top: 0;
      left: 0;
      width: 100%;
      height: 2px;
      transform: scaleY(0.5);
      transform-origin: 0 top;
    }
  }
}

/*四条border样式*/
@mixin all-border-1px ($color, $radius) {
  position: relative;
  border: none;
  &::after{
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    border: 2px solid $color;
    border-radius: $radius * 2;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    width: 200%;
    height: 200%;
    -webkit-transform: scale(0.5);
    transform: scale(0.5);
    -webkit-transform-origin: left top;
    transform-origin: left top;
  }

}
  • 使用
@import "@assets/style/mixin.scss";//引入

.box{
  @include all-border-1px(#eeeeee, 0); //使用
}

vuex的配置与使用

  • Vuex
import { toRefs, reactive } from "vue";
import { useStore } from "vuex";
export default {

  setup() {
    const state = reactive({
      name: ''
    })  
    const store = useStore()

    state.name = store.state.Name

    return {
      ...toRefs(state)
    }
  }
};
  • 替代方案 provide、inject

声明一次,全局可访问,将需要共享的数据事先在 Vue 的根节点 App.vue 中通过 provide 声明。 首先建立一个store

// src/store/store.ts
const planList = Symbol()
export default {
  planList,
}

在外层组件注入,比如 App.vue 中 provide

// src/App.vue
<script lang="ts">
import Store from "./store/store"

import { defineComponent, provide, ref } from "@vue/composition-api"
export default defineComponent({
  setup() {
    provide(Store.planList, ref([]))
  }
})
</script>

在需要的组件内inject接受

// src/views/Plan.vue
<script lang="ts">
import Store from "./store/store"

import { defineComponent, provide, ref } from "@vue/composition-api"
export default defineComponent({
  setup() {
    const planList = inject(Store.planList)
    return {
      planList
    }
  }
})
</script>

vue路由配置和缓存

  • keep-alive写法改变
 <router-view v-slot="{ Component }">
  <keep-alive>
    <component :is="Component" />
  </keep-alive>
</router-view>

tsconfig配置

把compileOnSave和sourceMap 设置成false,如果为true的话,在保存ts文件的时候会自动生成js和map文件

{
  "compileOnSave": false,
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": false,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

语法检测自动格式代码

  • eslintrc.js
module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: [
    "plugin:vue/vue3-essential",
    "eslint:recommended",
    "@vue/typescript/recommended",
    "@vue/prettier",
    "@vue/prettier/@typescript-eslint"
  ],
  parserOptions: {
    ecmaVersion: 2020
  },
  rules: {
    "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off"
  }
};

  • vscode settings.json
{
    // vscode默认启用了根据文件类型自动设置tabsize的选项
    "editor.detectIndentation": false,
    // 重新设定tabsize
    "editor.tabSize": 2,
    // #每次保存的时候自动格式化 
    "editor.formatOnSave": true,
    // #每次保存的时候将代码按eslint格式进行修复
    "eslint.autoFixOnSave": true,
    // 添加 vue 支持
    "eslint.validate": [
      "javascript",
      "javascriptreact",
      {
        "language": "vue",
        "autoFix": true
      }
    ],
    // #让prettier使用eslint的代码格式进行校验 
    "prettier.eslintIntegration": true,
    // #去掉代码结尾的分号 
    "prettier.semi": false,
    // #使用带引号替代双引号 
    "prettier.singleQuote": true,
    // #让函数(名)和后面的括号之间加个空格
    "javascript.format.insertSpaceBeforeFunctionParenthesis": true,
    // #这个按用户自身习惯选择 
    "vetur.format.defaultFormatter.html": "js-beautify-html",
    // #让vue中的js按编辑器自带的ts格式进行格式化 
    "vetur.format.defaultFormatter.js": "vscode-typescript",
    "vetur.format.defaultFormatterOptions": {
      "js-beautify-html": {
        "wrap_line_length": 120,
        "wrap_attributes": "auto"
        // #vue组件中html代码格式化样式
      }
    },
    // 格式化stylus, 需安装Manta's Stylus Supremacy插件
    "stylusSupremacy.insertColons": false, // 是否插入冒号
    "stylusSupremacy.insertSemicolons": false, // 是否插入分号
    "stylusSupremacy.insertBraces": false, // 是否插入大括号
    "stylusSupremacy.insertNewLineAroundImports": false, // import之后是否换行
    "stylusSupremacy.insertNewLineAroundBlocks": false,
    "explorer.confirmDelete": false // 两个选择器中是否换行
  }

发布&部署

  • 网站工具:https://vercel.com/
  • 用github账号登录(我项目是部署在guthub上的)
  • 点击import project -> import git repository
  • 输入自己的项目的git地址 https://xxx/xxx/xxx
  • 点击continue就会自动部署啦!部署好后会生成地址可以直接访问🍾

关于我

Image text 加我微信,邀你进入技术交流群,交流学习 😄 共同进步
如果喜欢请给我一个小♥♥ ⭐ (づ ̄3 ̄)づ

感谢

vue-h5-template

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].