微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Vue-meta 标签不使用 Rails 作为后端更新元标签

如何解决Vue-meta 标签不使用 Rails 作为后端更新元标签

我使用的是 Ruby 2.5.1Rails 6.0.3.4Vue 2.6

这里是导轨,

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>This is my back-end title</title>
    <%= csrf_Meta_tags %>
    <Meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
    <%= csp_Meta_tag %>
    <%= stylesheet_pack_tag 'application' %>
    <%= javascript_pack_tag 'application' %>
  </head>
  <body>
    <%= yield %>
  </body>
</html>

对于 vue:

javascript/packs/application.js

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import App from '../app.vue'
import router from '../routes'
import index from '../stores'
import Meta from 'vue-Meta'

Vue.use(Meta)
Vue.use(Vuex)

let token = document.getElementsByName('csrf-token')[0].getAttribute('content')
let Meta = document.createElement('Meta')
document.getElementsByTagName('head')[0].appendChild(Meta)
axios.defaults.headers.common['X-CSRF-Token'] = token
axios.defaults.headers.common['Accept'] = 'application/json'
document.addEventListener('DOMContentLoaded',() => {
  const el = document.body.appendChild(document.createElement('hello'))
  const app = new Vue({
    el,router,store: index,render: h => h(App),})
  console.log(app)
})

javascript/routes.js

import Vue from 'vue'
import VueRouter from 'vue-router'
.....
import AboutUs from './components/LandingPage/Footer/AboutUs.vue'
Vue.use(VueRouter)
export default new VueRouter({
  scrollBehavior() {
    return {x: 0,y: 0}
  },routes: [
    {
      path: '/AboutUs',name: 'AboutUs',component: AboutUs,},...
  ]
)}

AboutUs.vue

<template>
  <div>
    <p> I am about us page </p>
  </div>
</template>

<script>
export default {
  MetaInfo() {
    return {
      title: `About Us`,titleTemplate: '%s - test.com',Meta: [
        {name: 'keywords',content: this.keywords},{name: 'description',content: this.description,vmid: 'description'},{property: 'og:title',content: 'About Us',template: (chunk) => `${chunk} - test.com`,vmid: 'og:title'},{property: 'og:description',template: (chunk) => `${chunk}`,vmid: 'og:description'},],}
  },computed: {
    description() {
      return 'This is about us description' 
    },keywords() {
      return 'apple,ball,cat'
    },}
</script>

当我检查时,我可以看到:

<html>
  <head>
    <title>About Us - test.com</title>`
    <Meta name="csrf-param" content="authenticity_token">
    <Meta name="csrf-token" content="dLn5c24bcel/KwzvAwDSqBhG9EFmt6qwsah8yyLo1pUetoI2PpLLYF/cg4sfguF7DtXhYE0LKie9WJMmjNVdxQ==">
    <style> 
      ......
    </style>
    <Meta>
    <Meta data-vue-Meta="1" name="keywords" content= "apple,cat">
    <Meta data-vue-Meta="1" name="description" content="This is about us description" data- vmid="description">
    <Meta data-vue-Meta="1" property="og:title" content="About Us - test.com" data- vmid="og:title">
  </head>
</html>

在我的本地服务器上,我可以看到来自 vue 的更新标签

但是当我们进入生产阶段或https://developers.facebook.com/tools/debug/

http://test.com/#/AboutUs

I am getting og:title from backend which is:

这是我的后台标题

和警告如:

推断属性

'og:image' 属性应该明确提供,即使是一个值 可以从其他标签推断出来。

缺少属性

缺少以下必需的属性:og:url、og:type、 og:title,og:image,og:description,fb:app_id

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?