有一些选项可以将功能全局应用于您的 vuepress 项目。
你可以在每个组件中添加你的辅助函数作为 mixins,或者在enhanceApp.js
文件中全局定义它们。例如,在下面的代码中,我全局定义了helperFunction()
方法
// enhanceApp.js
export default ({
Vue, // the version of Vue being used in the VuePress app
options, // the options for the root Vue instance
router, // the router instance for the app
siteData, // site metadata
isServer // is this enhancement applied in server-rendering or client
}) => {
Vue.mixin({
methods: {
helperFunction () {
// Your helper function
}
}
})
}
这个问题有一个很好的例子,在单个文件组件而不是全局范围中定义 mixin。您可以在Vue.js 文档中找到有关 mixins 的更多信息。
另一种方法是将您的辅助函数分离到 vuepress 插件中。