nuxt.js - 加载 og :image from assets in nuxt. config.js

标签 nuxt.js

有没有办法导入图像并将其应用于 og:image 元标记?
我有以下 nuxt.config.js:

module.exports = {
  mode: 'universal',

  head: {
    title: 'title name',
    meta: [
      // { hid: 'og:image', property: 'og:image', content: process.env.BASE_URL + ogImage },
    ],
  },

  // ...
};

在普通的 vue 项目中,这可以通过使用 import 轻松完成:
import ogImage from '@/path/to/image.png';

但是,nuxt.config.js 中没有 import 语句,使用 require 只会导致加载二进制文件,而不是 Assets 路径。

最佳答案

从 Nuxt 2.0 开始,nuxt 配置中提供了导入语句。 See release notes .但这行不通,因为还没有装载机和其他东西。

您可以在布局文件中设置它。

  import ogImage from '@/path/to/image.png';
  export default {
  head () {
    return {
      meta: [
        { hid: 'og:image', property: 'og:image', content: this.BASE_URL+ ogImage }
      ]
    }
  },

关于nuxt.js - 加载 og :image from assets in nuxt. config.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52683062/

相关文章:

vue.js - Nuxt Fetch 在首次加载时不会更新

javascript - 组件名称无效 : "pages/product/_slug.vue". 组件名称应符合 html5 规范中有效的自定义元素名称

node.js - 如何在真实服务中运行nuxt.js?

vue.js - 如何在 .onRequest 拦截器中使用 axios helper setToken

nuxt.js - 如何在 Nuxt 的 composition api 中使用 vuex map helpers

nuxt.js - 出现错误 "defineNuxtConfig is not defined"

vue.js - Nuxt Vuetify 按钮始终悬停

nuxt.js - 如何在生产模式下使 Nuxt Content 热重载工作

javascript - 如何遍历模板中的组件对象?

javascript - 如何在nuxt中使用amchart?