Markdown-它不适用于 Nuxt 和内容丰富,抛出错误

如何解决Markdown-它不适用于 Nuxt 和内容丰富,抛出错误

我正在使用 Contentful 构建网站,并遵循了不同的指南和配置。无论我做什么,我似乎都无法获得 Markdownit 工作的基本示例。我一直收到错误:

属性或方法“$md”未在实例上定义,但在渲染期间被引用。

无法在文档中获取任何示例: https://openbase.com/js/@nuxtjs/markdownit

我什至删除了整个模板并尝试了简单的版本。我还在下面的配置文件中尝试了不同的设置。

我已经运行了'yarn build'、'yarn add markdown-it'

而且似乎都没有帮助

这是我最终想要开始工作的内容:

<article v-html="$md.render(post.fields.body)"></article>

这是我正在关注的一个示例,它似乎在现场演示中起作用: https://github.com/sdras/contentful-nuxt-netlify/blob/e80e6552eef812320a7bd2dd66ad3fa8ebf5f840/pages/_slug.vue

上下文 - pages/_id/index.vue

<template>
  <div class="post-component">
    <a @click="$router.go(-1)">Go back to overview</a>
    <hr />
    <h1>{{post.fields.title}}</h1>
    <p></p>

    <p>
      {{post.fields.body}}
    </p>

    <article v-html="$md.render(post.fields.body)"></article>
    
  </div>
</template>

<script>
  import {createClient} from '../../plugins/contentful';
  const contentfulClient = createClient();

  export default {
    name: 'index',asyncData ({ env,params }) {
      return contentfulClient.getEntries({
        'content_type': env.CTF_BLOG_POST_TYPE_ID,'fields.slug': params.id
      }).then(post => {
        return {
          post: post.items[0]
        }
      }).catch(console.error)
    }
  }
</script>

nuxt.config.js

const config = require('./.contentful.json')


module.exports = {
  // ...
  env: {
    CTF_SPACE_ID: config.CTF_SPACE_ID,CTF_CDA_ACCESS_TOKEN: config.CTF_CDA_ACCESS_TOKEN,CTF_PERSON_ID: config.CTF_PERSON_ID,CTF_BLOG_POST_TYPE_ID: config.CTF_BLOG_POST_TYPE_ID
  }
  // ...
}

export default {
  // Target: https://go.nuxtjs.dev/config-target
  target: 'static',// Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    title: 'thomasulman',htmlAttrs: {
      lang: 'en'
    },meta: [
      { charset: 'utf-8' },{ name: 'viewport',content: 'width=device-width,initial-scale=1' },{ hid: 'description',name: 'description',content: '' },{ name: 'format-detection',content: 'telephone=no' }
    ],link: [
      { rel: 'icon',type: 'image/x-icon',href: '/favicon.ico' }
    ]
  },// Global CSS: https://go.nuxtjs.dev/config-css
  css: [
  ],// Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
  ],// Auto import components: https://go.nuxtjs.dev/config-components
  components: true,// Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [
  ],// Modules: https://go.nuxtjs.dev/config-modules
  modules: [

  'nuxt-buefy',"@nuxtjs/markdownit"],markdownit: {
    runtime: true,// Support `$md()`
    injected: true,},// Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
  }
}

package.json

{
  "name": "******","version": "1.0.0","private": true,"scripts": {
    "dev": "nuxt","build": "nuxt build","start": "nuxt start","generate": "nuxt generate"
  },"dependencies": {
    "@nuxtjs/markdownit": "^1.2.10","contentful": "^8.4.2","core-js": "^3.15.1","markdown-it": "^12.1.0","nuxt": "^2.15.7","nuxt-buefy": "^0.4.8"
  },"devDependencies": {}
}

以防万一它有帮助: 索引.vue

<template>
  <div>
    <!-- <Navigation /> -->
    <!-- render data of the person -->
    <h1>{{ person.fields.name }}</h1>
    <!-- render blog posts -->
    <ul>
      <li v-for="post in posts">
        {{ post.fields.title }}
        <nuxt-link :to="post.fields.slug" class="more">Read more ⟶</nuxt-link>
      </li>
    </ul>
  </div>
</template>

<script>
import { createClient } from '~/plugins/contentful.js'
const client = createClient()
export default {
  // `env` is available in the context object
  asyncData({ env }) {
    return Promise.all([
      // fetch the owner of the blog
      client.getEntries({
        'sys.id': env.CTF_PERSON_ID
      }),// fetch all blog posts sorted by creation date
      client.getEntries({
        'content_type': env.CTF_BLOG_POST_TYPE_ID,order: '-sys.createdAt'
      })
    ]).then(([entries,posts]) => {
      // return data that should be available
      // in the template
      return {
        person: entries.items[0],posts: posts.items
      }
    }).catch(console.error)
  }
}
</script>

解决方法

我已实现使其正常工作,如下所示:https://github.com/nuxt-community/markdownit-module#usage

我也添加了一个 proper HTML sanitizer,检查插件目录。

您可以在此处找到可用的 Github 存储库:https://github.com/kissu/so-nuxt-markdownit

目前这个配置对我来说已经足够了
nuxt.config.js

plugins: [
  '~/plugins/vue-dompurify'
],modules: [
  '@nuxtjs/markdownit'
],markdownit: {
  runtime: true,// Support `$md()`
  // preset: 'default',// linkify: true,// breaks: true,// use: [
  //   'markdown-it-div',//   'markdown-it-attrs'
  // ]
}

并且这两个文件工作正常
index.vue

<template>
  <div>
    <div v-dompurify-html="$md.render(model)"></div>

    <br/>
    <br/>

    <div v-dompurify-html="testMarkdownContent"></div>

    <nuxt-link to="/inline">Inline markdown working too!</nuxt-link>
  </div>
</template>

<script>
import testMarkdown from '../blog/test.md'

export default {
  data() {
    return {
      model: '# Hello World!'
    }
  },computed: {
    testMarkdownContent() {
      return testMarkdown
    }
  }
}
</script>

inline.vue

<template lang="md">
  # Hello World!

  Current route is: {{ $route.path }}

  <nuxt-link to="/">Back to index</nuxt-link>
</template>
,

整个事情的问题在于 // module.exports {} 覆盖了配置文件中的导出默认 {}。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-