简体   繁体   English

NativeScript + Vue.js + FontAwesome

[英]NativeScript + Vue.js + FontAwesome

I'm trying to use FontAwesome icon set to build an application over NativeScript and Vue.js but I can't figure out the problem since I not even have an error prompt message.我正在尝试使用 FontAwesome 图标集在 NativeScript 和 Vue.js 上构建应用程序,但我无法弄清楚问题,因为我什至没有错误提示消息。

I'm following the documentation as a faithful but nothing happens.我忠实地遵循文档,但没有任何反应。 I search everywhere but.. nothing.我到处搜索,但......什么都没有。

If you can help me with this I'd be so grateful.如果你能帮我解决这个问题,我将不胜感激。

Documentation: https://nativescript-vue.org/blog/using-fonticons/文档: https : //nativescript-vue.org/blog/using-fonticons/

Thanks!谢谢!

SOLUTION解决方案

  • Create a folder named fonts in the app folderapp文件夹中创建一个名为fonts的文件夹
  • Generate a style file and put the following code生成样式文件,放入如下代码

 .fa { font-family: 'Font Awesome 5 Free', 'font-awesome/fa-regular-400'; } .fas { font-family: 'Font Awesome 5 Free', 'font-awesome/fa-solid-900'; } .fab { font-family: 'Font Awesome 5 Free', 'font-awesome/fa-brands-400'; }

The font-family first part is to name the font family, and after the comma, we will write the path to those fonts. font-family 第一部分是命名字体系列,在逗号之后,我们将写出这些字体的路径。

To get the Brands and Pro FontAwesome 5 fonts working in NativeScript-Vue, install nativescript-fonticon , npm install nativescript-fonticon --save , as described in the documentation Using Fonticons , and from FontAwesome download both the webfonts and desktop fonts.要让 Brands 和 Pro FontAwesome 5 字体在 NativeScript-Vue 中工作,请安装nativescript-fonticonnpm install nativescript-fonticon --save ,如文档Using Fonticons 中所述,并从 FontAwesome 下载 webfonts 和桌面字体。 In the directory app/fonts add the .ttf files from the webfonts directory of the web download.在目录app/fonts添加来自 Web 下载的webfonts目录中的.ttf文件。 iOS also requires the .otf files from the otfs directory of the desktop download so add these to the app/fonts directory as well and rename the basename of the .otf files to match the corresponding basename of the .ttf file iOS 还需要桌面下载的otfs目录中的.otf文件,因此也将这些文件添加到app/fonts目录中,并重命名.otf文件的 basename 以匹配.ttf文件的相应 basename 在此处输入图片说明

In app/assets add the corresponding css files from the css directory of the web font download (or the all file).app/assets ,从网络字体下载(或所有文件)的css目录中添加相应的 css 文件。 在此处输入图片说明

Now add the following to app.scss (note that light and solid don't work properly without font-weight properly defined)现在将以下内容添加到app.scss (请注意,如果没有正确定义font-weight ,light 和 solid 将无法正常工作)

.fa {
  font-family: "Font Awesome 5 Pro", "fa-regular-400";
  font-weight: 400;
}

.fas {
  font-family: "Font Awesome 5 Pro", "fa-solid-900";
  font-weight: 900;
}

.fab {
  font-family: "Font Awesome 5 Brands", "fa-brands-400";
  font-weight: 400;
}

.fal {
  font-family: "Font Awesome 5 Pro", "fa-light-300";
  font-weight: 300;
}

and the following to main.ts和以下到main.ts

import {TNSFontIcon, fonticon} from 'nativescript-fonticon';

TNSFontIcon.debug = true;
TNSFontIcon.paths = {
  // 'fa': './assets/css/all.min.css',
  // 'fal': './assets/css/all.min.css',
  // 'far': './assets/css/all.min.css',
  // 'fas': './assets/css/all.min.css',
  // 'fab': './assets/css/all.min.css',
  'fa': './assets/css/fontawesome.min.css',
  'fal': './assets/css/light.min.css',
  'far': './assets/css/regular.min.css',
  'fas': './assets/css/solid.min.css',
  'fab': './assets/css/brands.min.css'
};
TNSFontIcon.loadCss();

Vue.filter('fonticon', fonticon);

Now delete the platforms directory to make sure everything gets bundled correctly and you should be good to go.现在删除platforms目录以确保所有内容都正确捆绑,您应该很高兴。 Use it like使用它就像

  <StackLayout orientation="horizontal" horizontalAlignment="center" verticalAlignment="top">
    <Label class="fab" :text="'fa-git' | fonticon" />
    <Label class="fal" :text="'fa-plus-square' | fonticon" />
    <Label class="fa" :text="'fa-plus-square' | fonticon" />
    <Label class="fas" :text="'fa-plus-square' | fonticon" />
  </StackLayout>

To make things even easier there is plugin Vue-Fonticon that is essentially the following code which I copied into app/components/FontIcon.vue为了让事情变得更容易,有一个插件Vue-Fonticon ,它本质上是我复制到app/components/FontIcon.vue的以下代码

<template>
  <Label
    :class="type"
    :color="color"
    :fontSize="size"
    :text="name | fonticon"
    :width="size"
    textAlignment="center"
    @tap="$emit('tap')"
  />
</template>

<script>
  export default {
    name: 'FontIcon',
    props: {
      color: {
        type: String,
        default: 'black'
      },
      name: {
        type: String,
        required: true
      },
      size: {
        type: [String, Number],
        default: 15,
        validation: s => !isNaN(s)
      },
      type: {
        type: String,
        default: 'fa'
      }
    }
  }
</script>

To use it, in main.ts add要使用它,在main.ts添加

import FontIcon from './components/Utility/FontIcon.vue'

Vue.component(FontIcon.name, FontIcon)

and use it as并将其用作

  <StackLayout orientation="horizontal" horizontalAlignment="center" verticalAlignment="top">
    <FontIcon name="fa-play"/>
    <FontIcon name="fa-play" type="fal"/>
    <FontIcon name="fa-play" type="fas"/>
    <FontIcon name="fa-git" type="fab"/>
  </StackLayout>

It worked for me using FontAwesome 4.7.0.它使用 FontAwesome 4.7.0 对我有用。 Since version 5.0, the packaging of font awesome changed and using ttf is not the recommended way anymore, perhaps using font awesome 5.x is more tricky in nativescript-vue.从 5.0 版本开始,font awesome 的包装发生了变化,不再推荐使用 ttf 的方式,也许在 nativescript-vue 中使用 font awesome 5.x 更加棘手。

You can download it there : https://fontawesome.com/v4.7.0/你可以在那里下载: https : //fontawesome.com/v4.7.0/

Use fontawesome-webfont.ttf as explained in the doc and rename it to FontAwesome.ttf, copy font-awesome.min.css too按照文档中的说明使用 fontawesome-webfont.ttf 并将其重命名为 FontAwesome.ttf,也复制 font-awesome.min.css

in your app.scss, don't forget this (the font family is the name of your ttf file)在您的 app.scss 中,不要忘记这一点(字体系列是您的 ttf 文件的名称)

.fa {
   font-family: FontAwesome;
}

And remember to check your icon names on the v4.7, some are new in v5并记得在 v4.7 上检查您的图标名称,有些是 v5 中的新名称

In debug mode, when your app starts, you should see :在调试模式下,当您的应用程序启动时,您应该看到:

JS: 'Collections to load: fa'
JS: '----------'
JS: 'Loading collection \'fa\' from file: ./fonts/FontAwesome.css'
JS: 'fa-glass: \\uf000'
JS: 'fa-music: \\uf001'
JS: 'fa-search: \\uf002'
JS: 'fa-envelope-o: \\uf003'
...

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

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