简体   繁体   English

为VueJS应用实现manifest.json文件以设置条件

[英]Implementing a manifest.json file for a VueJS app to set conditions

I have added a manifest.json file to my Vue app. 我已经将manifest.json文件添加到我的Vue应用程序中。 This file lives in a folder called scripts which is then referenced on my main.js. 该文件位于一个名为scripts的文件夹中,然后在我的main.js中进行引用。 However, it really doesn't do anything. 但是,它实际上没有任何作用。 For example, I have it set up to set the display as standalone, fix the orientation to portrait and also add a title and bookmark icons when the user adds it to bookmark in their browser. 例如,我将其设置为将显示设置为独立显示,将方向固定为纵向,并在用户将其添加到浏览器中的书签时添加标题和书签图标。 Unfortunately, for some reason it doesn't seem to work at all. 不幸的是,由于某种原因,它似乎根本不起作用。 Where have I gone wrong? 我哪里出问题了?

The manifest.json is as follows manifest.json如下

{
  "name": "Bake Bread App",
  "short_name": "Breadr",
  "theme_color": "blue",
  "background_color": "blue",
  "display": "standalone",
  "orientation": "portrait",
  "Scope": "/",
  "start_url": "/",
  "icons": [
    {
      "src": "/assets/bookmark-icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png"
    },
    {
      "src": "/assets/bookmark-icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ],
  "splash_pages": null
}

Then if we go up a level (out of this scripts folder) and to main.js, I have 然后,如果我们上一个级别(不在此scripts文件夹中)并转到main.js,我有

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

Vue.config.productionTip = false;

// FILTERS
Vue.filter('snippet', function(value) {
    return value.slice(0,100);
});

Vue.component('loading-screen', {
  template: '<div id="loading">Loading...</div>'
});

import json from './scripts/manifest.json'
export default{
    data(){
        return{
            myJson: json
        }
    }
}

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount("#app");

In addition, I added <link rel="manifest" href="js/manifest.json"> in my index.html as instructed via the Google Developers Documentation. 此外,按照Google Developers Documentation的指示,我在index.html中添加了<link rel="manifest" href="js/manifest.json">

The reason it is not working in Vue img src neds to be defined in the Data object with require() 在Vue img src中无法使用require()Data object定义的原因

and webpack needs to know about the images. 和webpack需要了解图像。

  • sandbox 砂箱

  • you can modify your manifest and use a getter function for your resources. 您可以修改清单并为资源使用getter函数。

  • Or you can use require("path to the resource (not the manifest string)") in your data object. 或者,您可以在数据对象中use require("path to the resource (not the manifest string)")

helloWorld.vue: helloWorld.vue:

 <template>
    <div class="hello">
    <h1>{{ msg }}</h1>

    <ol v-for="x in myJson.icons" :v-bind="x" :key="x">
      <li>
      <div> name: {{x.name}}</div>
      <div> name: {{x.sizes}}</div>
      <div> name: {{x.type}}</div>
      <img :src="getSrc(x.src)" alt="wrong..">
      </li>
    </ol>
  </div>
</template>

<script>
import json from "../../manifest.json";

export default {
  name: "HelloWorld",
  data() {
    return {
      myJson: json,
      msg: "Manifest:"
    };
  },
  methods: {
    getSrc(src) {
      return require("../assets/" + src);
    }
  }
};
</script>

modified icon json: 修改后的图标json:

  "icons": [
    {
      "name": "logo",
      "src": "logo.png",
      "sizes": "99x99",
      "type": "image/png"
    },
    {
      "name": "logo2",
      "src": "logo.png",
      "sizes": "929x929",
      "type": "image/png"
    }
  ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM