Git Product home page Git Product logo

catalogue's Introduction

说明

这是一个文章目录组件,用于生成文章的目录,并附带平滑滚动和目录定位功能。

示例

目录示例

快速开始

npm install --save article-catalogue
<template>
  <div>
    <div id="article" v-html="content" />
    <Catalogue selector="#article" />
  </div>
</template>

<script>
import Catalogue from 'article-catalogue'
export default {
  components: { Catalogue }
}
</script>

配置项

样式采用stylus,使用前请确保安装相应的依赖。默认配置一共三项,组件部分细节如下:

export default {
    ···
	props: {
        selector: {
          type: String,
          required: true // 必须项,文章元素对应的选择器
        },
        title: {
          type: String,
          default: '目录'
        },
        asyncData: {
          type: Boolean,
          default: true
        }
	},
    ···
}

关于asyncData配置项的说明:一般来说,文章的内容都是通过ajax请求异步获取并渲染到页面上的,这意味着页面初始化时,目录组件是无法获取到dom元素的。以下是内部实现细节,通过父组件的updated钩子来触发目录组件的初始化。

···
mounted () {
    if (this.asyncData) {
        this.$parent.$once('hook:updated', this.init)
    } else {
        this.init()
    }
}
···

当然,如果你的文章内容是同步数据(即dom节点在组件初始化时已经存在在页面中),那么您可以通过设置:asyncData: ture

<Catalogue selector="#article" :asyncData="false" />

当然,组件十分简单,总共不过200行代码,除去模板和样式,也许只有100行。如果您愿意,完全可以将组件从node_modules中拿出来自行修改。

catalogue's People

Contributors

emosheeep avatar

Stargazers

fenzsen avatar lrk avatar  avatar

Watchers

 avatar

Forkers

themenote

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.