Giter VIP home page Giter VIP logo

vue3-h5-template's Introduction

vue3-template

基于 vue3 + vite + Pinia + quark design + sass/less + viewport 适配方案 + axios 封装,构建手机端模板脚手架

启动项目

npm install
npm run dev

目录

✅ 配置多环境变量

package.json 里的 scripts 配置 dev dev:test dev:prod ,通过 --mode xxx 来执行不同环境

  • 通过 npm run dev 启动本地环境参数 , 执行 development
  • 通过 npm run dev:test 启动测试环境参数 , 执行 test
  • 通过 npm run dev:prod 启动正式环境参数 , 执行 prod
"scripts": {
    "dev": "vite",
    "dev:test": "vite --mode test",
    "dev:prod": "vite --mode production",
}

▲ 回顶部

✅ viewport 适配方案

不用担心,项目已经配置好了 viewport 适配, 下面仅做介绍:

PostCSS 配置

下面提供了一份基本的 postcss 配置,可以在此配置的基础上根据项目需求进行修改

// https://github.com/michael-ciniawsky/postcss-load-config
module.exports = {
  plugins: {
    'postcss-px-to-viewport-8-plugin': {
      unitToConvert: 'px', // 要转化的单位
      viewportWidth: 375, // UI设计稿的宽度
      unitPrecision: 6, // 转换后的精度,即小数点位数
      propList: ['*'], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
      viewportUnit: 'vw', // 指定需要转换成的视窗单位,默认vw
      fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位,默认vw
      minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换
      mediaQuery: true, // 是否在媒体查询的css代码中也进行转换,默认false
      replace: true, // 是否转换后直接更换属性值
      exclude: [/node_modules/], // 设置忽略文件,用正则做目录名匹配
    },
  },
};

更多详细信息: quarkd

▲ 回顶部

✅ quarkd 组件按需加载

https://quark-ecosystem.github.io/quarkd-docs

安装插件

npm i quarkd

▲ 回顶部

✅ Pinia 状态管理

下一代 vuex,使用极其方便,ts 兼容好

目录结构

├── store
│   ├── modules
│   │   └── user.js
│   ├── index.js

使用

<script lang="ts" setup>
  import { useUserStore } from '@/store/modules/user';

  const userStore = useUserStore();
  userStore.login();
</script>

▲ 回顶部

✅ Vue-router

本案例采用 hash 模式,开发者根据需求修改 mode base

注意:如果你使用了 history 模式, vue.config.js 中的 publicPath 要做对应的修改

前往:vue.config.js 基础配置

import Vue from 'vue';
import { createRouter, createWebHistory, Router } from 'vue-router';

Vue.use(Router);

export const router = [
  {
    name: 'root',
    path: '/',
    redirect: '/home',
    component: () => import('@/layout/basic/index.vue'),
  },
];

const router: Router = createRouter({
  history: createWebHistory(),
  routes: routes,
});

export default router;

更多:Vue Router

▲ 回顶部

✅ Axios 封装及接口管理

utils/request.js 封装 axios , 开发者需要根据后台接口做修改。

  • service.interceptors.request.use 里可以设置请求头,比如设置 token
  • config.hideloading 是在 api 文件夹下的接口参数里设置,下文会讲
  • service.interceptors.response.use 里可以对接口返回数据处理,比如 401 删除本地信息,重新登录
import axios from 'axios';
import store from '@/store';
import Toast from 'quarkd/lib/toast';
// 根据环境不同引入不同api地址
import { baseApi } from '@/config';

// create an axios instance
const service = axios.create({
  baseURL: baseApi, // url = base api url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000, // request timeout
});

// request 拦截器 request interceptor
service.interceptors.request.use(
  (config) => {
    // 不传递默认开启loading
    if (!config.hideloading) {
      // loading
      Toast.loading('loading');
    }
    if (store.getters.token) {
      config.headers['X-Token'] = '';
    }
    return config;
  },
  (error) => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  },
);
// respone拦截器
service.interceptors.response.use(
  (response) => {
    Toast.clear();
    const res = response.data;
    if (res.status && res.status !== 200) {
      // 登录超时,重新登录
      if (res.status === 401) {
        store.dispatch('FedLogOut').then(() => {
          location.reload();
        });
      }
      return Promise.reject(res || 'error');
    } else {
      return Promise.resolve(res);
    }
  },
  (error) => {
    Toast.clear();
    console.log('err' + error); // for debug
    return Promise.reject(error);
  },
);
export default service;

接口管理

src/api 文件夹下统一管理接口

  • 你可以建立多个模块对接接口, 比如 home.js 里是首页的接口这里讲解 user.js
  • url 接口地址,请求的时候会拼接上 config 下的 baseApi
  • method 请求方法
  • data 请求参数 qs.stringify(params) 是对数据系列化操作
  • hideloading 默认 false, 设置为 true 后,不显示 loading ui 交互中有些接口不需要让用户感知
import qs from 'qs';
// axios
import request from '@/utils/request';
//user api

// 用户信息
export function getUserInfo(params) {
  return request({
    url: '/user/userinfo',
    method: 'post',
    data: qs.stringify(params),
    hideloading: true, // 隐藏 loading 组件
  });
}

如何调用

// 请求接口
import { getUserInfo } from '@/api/user.js';

const params = {
  user: 'sunnie',
};
getUserInfo(params)
  .then(() => {})
  .catch(() => {});

▲ 回顶部

✅ vite.config.ts 基础配置

如果你的 Vue Router 模式是 hash

publicPath: './',

如果你的 Vue Router 模式是 history 这里的 publicPath 和你的 Vue Router base 保持一直

publicPath: '/app/',
export default function ({ command }: ConfigEnv): UserConfigExport {
  const isProduction = command === 'build';
  return {
    server: {
      host: '0.0.0.0',
    },
    plugins: [
      vue(),
      vueJsx(),
      viteMockServe({
        mockPath: './src/mock',
        localEnabled: command === 'serve',
        logger: true,
      }),
    ],
  };
}

▲ 回顶部

✅ 配置 alias 别名

resolve: {
    alias: [{
            find: 'vue-i18n',
            replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
        },
        // /@/xxxx => src/xxxx
        {
            find: /\/@\//,
            replacement: pathResolve('src') + '/',
        },
        // /#/xxxx => types/xxxx
        {
            find: /\/#\//,
            replacement: pathResolve('types') + '/',
        },
    ],
},

▲ 回顶部

✅ 配置 proxy 跨域

server: {
    proxy: {
        '/api': {
            target: 'https://baidu.com',
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '')
        }
    }
},

▲ 回顶部

✅ Eslint+Pettier+stylelint 统一开发规范

根目录下的.eslintrc.js.stylelint.config.js.prettier.config.js内置了 lint 规则,帮助你规范地开发代码,有助于提高团队的代码质量和协作性,可以根据团队的规则进行修改

vue3-h5-template's People

Contributors

bxychy avatar dependabot[bot] avatar xsf0105 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue3-h5-template's Issues

请问发送请求后如何获取服务器返回的数据?

如题:

/** 
     * 根据用户输入的提问,向知识库获取回答
     * @param question 用户提问
     * @return UseAxiosReturn 知识库回答
     */
    function getAnswerList(id: string, question: string) {
        useAxiosApi(`/knowledgebase/${id}/search`, {
            method: 'GET',
            params: { query: question },
            headers: {
                'Content-Type': 'application/json'
            }
        }).then((res) => {
            console.log(res)
            //请问这里要怎么样才能取出数据?
        }).catch(() => {

        });
    }

打印res之后的内容为:

{
    "code": 200,
    "items": [
        {
            "content": "content",
            "identify": "1767763096417337344",
            "metadata": {
                "seq_num": 54
            },
            "score": 0.7338999207181424
        },
        {
            "content": "content2",
            "identify": "1767763096417337344",
            "metadata": {
                "seq_num": 12
            },
            "score": 0.6277636422458995
        },
    ],
    "totalCount": 10
}

res的类型为res: StrictUseAxiosReturn<any, AxiosResponse<any, any>, any>,那我怎么样才能取到我想要的items里的内容呢。还没有学ts就开始写项目了,但时间又比较紧,感谢大佬了

组件库的选型

请问作者用的是vant还是nutUI呢,看介绍怎么感觉都在用呢?

axios...

看到使用了axios,然后搜索了一下,发现只有一句import axios.....震惊了,,,,

如何把这项目放上production

(。・∀・)ノ゙嗨你好,我目前正在试着学做Vue2 SPA, 在github上找到这项目,clone了后在电脑上可以运行,可是我想把这项目放上服务器(我用着heroku, 目前只会这个),可是却发现application error

2017-05-19T03:22:41.424890+00:00 app[web.1]: npm ERR! Exit status 1 2017-05-19T03:22:41.406018+00:00 app[web.1]: at tryModuleLoad (module.js:447:12) 2017-05-19T03:22:41.426455+00:00 app[web.1]: npm ERR! A complete log of this run can be found in: 2017-05-19T03:22:41.424455+00:00 app[web.1]: npm ERR! code ELIFECYCLE 2017-05-19T03:22:41.406018+00:00 app[web.1]: at Function.Module._load (module.js:439:3) 2017-05-19T03:22:41.426575+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2017-05-19T03_22_41_413Z-debug.log 2017-05-19T03:22:41.525580+00:00 heroku[web.1]: State changed from starting to crashed 2017-05-19T03:23:09.679532+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simplevue.herokuapp.com request_id=9644c2a8-e87f-4856-90ac-82a2ddf8bb6c fwd="121.122.29.117" dyno= connect= service= status=503 bytes= protocol=https 2017-05-19T03:23:10.126194+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=simplevue.herokuapp.com request_id=2b71a5c2-3e42-4869-96b4-77c83f84fdbd fwd="121.122.29.117" dyno= connect= service= status=503 bytes= protocol=https 2017-05-19T03:25:56.731608+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=simplevue.herokuapp.com request_id=57c16b61-c09b-4d65-a0aa-32abb3dc6601 fwd="121.122.29.117" dyno= connect= service= status=503 bytes= protocol=https 2017-05-19T03:25:57.474399+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=simplevue.herokuapp.com request_id=a309e637-0d9f-42e9-ad04-1393e92913af fwd="121.122.29.117" dyno= connect= service= status=503 bytes= protocol=https

查看了log,不是很懂,可以请问大大这是什么问题吗?

新建vue文件报错

当在view 里面新建一个文件夹,然后新建一个vue文件时候,ts 会提示错误,首次编译没有问题,当npm run dev启动一次后,再执行新建文件操作,比如 view/testPage/index.vue,里面直接copy一份src/views/home/index.vue的代码,会有各种ts的报错,autoImport 识别不了,感觉tsconfig的内容不会对新建的vue 文件生效,不知道是缓存还是什么,我这里100% 复现

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.