npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xohu/vue-cli-plugin-create-router

v1.4.5

Published

vue-cli3 plugin

Downloads

464

Readme

@xohu/vue-cli-plugin-create-router

vue-cli3 的 create-router 插件

  • 安装

    vue add @xohu/create-router
    or
    npm install @xohu/vue-cli-plugin-create-router -D
    or
    cnpm install @xohu/vue-cli-plugin-create-router -D

注入的命令

  • vue-cli-service createRouter

    使用:vue-cli-service createRouter [options]
    
    选项:
      --async 开启路由异步模式,实现路由组件的懒加载 (默认不开启)
    ## 注
    create-router 插件对 vue-cli-service serve 增加了 --watch 监听模式
    可以运行 npx vue-cli-service help serve 查看所有选项
    如有需要,可以监听模式运行 serve 服务,监听 projectPath 目录下文件的 created,removed,changed

配置

可以通过 vue.config.js > pluginOptions.createRouterConfig 进行配置

// vue.config.js
module.exports = {
  pluginOptions: {
       createRouterConfig: {
          // 工作目录(默认: src)
          cwd: path.resolve(process.cwd(), './src'),
          // 项目页面地址(默认: views)
          projectPath: `views`,
          // 生成路由文件的名称(默认: index)
          outputFileName: 'index',
          // 忽略的页面文件(不被写入 router 配置)(默认:[])(例:['login', 'header.vue'])
          ignore: [],
          // 开启路由异步模式(默认: true)
          async: true,
          // 以监听模式运行 serve 服务,监听 projectPath 目录下文件的 created,removed,changed (默认: true)
          watch: true,
          // 以监听模式运行 serve 服务,监听 projectPath 目录下文件的 changed (默认: watch 的值)
          changeWatch: true
      }
   }
}

项目目录

.
├─assets
├─components
├─views
│  ├─layout
│  │   └─home.vue
│  └─layout.vue,
│  └─login.vue,
├─router
│  └─index.js
└─store

创建内嵌子路由,你需要添加一个 vue 文件,同时添加一个与该文件同名的目录用来存放子视图组件

使用 <router-config> 标签 可以对每个页面进行单独配置,并且会覆盖 createRouterConfig 中的全局配置,更多路由介绍请查看 vue-router 官网

# views/layout.vue
<router-config>
{
    // 注释说明文字
    note: 'Layout 页面',
    // 是否忽略当前文件(写入 router 配置)(true:忽略,false:不忽略)
    ignore: false,
    // 开启路由异步模式(默认: true)
    async: true,
    // 监听当前页面内容的 changed (默认: watch 的值)
    changeWatch: true,

    // 以下参数请参考 vue-router 官网
    path: '/layout',
    alias: '',
    redirect: '',
    meta: {
        code...
    },
    beforeEnter: function(to, from, next) {
        code...
    }
}
</router-config>

<template>
    <div>
        hello
    </div>
</template>

<script>
export default {
    name: 'layout',
    mounted() {
        this.$nextTick(() => {
            // 获取 <router-config> 标签的内容
            console.log(this.$options._router)
        });
    }
}
</script>

<route-config> 包含一个 json 的内容, 其中字段会同步到生成的 router/index.js 路由配置文件

启动项目后生成文件router/index.js


// Layout 页面
const layout = () => import('@/views/layout')
// Home 页面
import layoutHome from '@/views/layout/home'
// 登录 页面
import login from '@/views/login'

const routes = [
    {
        name: "/layout",
        path: "/layout",
        component: layout,
        children: [
        {
            name: "/layout/home",
            path: "home",
            component: layoutHome
        }
        ],
        meta: {
            requiresAuth: true
        },
        redirect: {
            name: "/test"
        },
        beforeEnter: function(to, from, next) {
            next()
        }
    },
    {
        name: "/login",
        path: "/login",
        component: login
    }
]

export default routes
// main.js
import Vue from 'vue'
import App from './App.vue'
import Router from 'vue-router' 
Vue.use(Router) 

import routes from '@/router' // @:src

const router = new Router({
    routes
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

路由文件生成规则

路由 name 和 path 默认按照文件路径命名,例如 /layout/test1/test2/home
{
    name: "/layout/test1/test2/home",
    path: "/layout/test1/test2/home",
    component: layoutTest1Test2Home
}

注意事项

默认 <route-config> 标签的内容发生改变后自动触发更新路由配置文件.
可以在 createRouterConfig 全局或者单个文件上设置 changeWatch:true | false 开启或关闭.
[ ] { } # % $ ^ * + = - | ~ < > . , ? ! @ & ( ) : ; " & \\ @
以上关键字命名的 vue 文件,会自动过滤掉关键字
/layout/test1/test2/h@#$%~^&*ome = /layout/test1/test2/home