簡単であるVUEJSにおけるSVGSの使用
ウェブのイメージ
ウェブで最も一般的に使用されるイメージ形式はビットマップ、SVG、およびWebPを含んでいます.
ビットマップ
ビットマップ画像は、ピクセルと呼ばれる小さなドットから構成されます.各画素は、実際には正方形であり、特定の色を割り当て、パターンを配置して画像を形成する.ときに、実際には、各ピクセルを参照することができますビットマップでズーム.JPEG、PNG、GIFのようなイメージ形式はすべてビットマップです.ビットマップ形式は、写真のような非常に詳細なイメージに適しています.
Webp
WebP それはビットマップを置き換えるために作成されたGoogleによって作成された非常に素晴らしいイメージ形式です.
SVG
スケーラブルなベクトルグラフィックス(SVG)は、彼らがレンダリング装置で計算されることができる方法についての情報でバンドルされない数学式からつくられるベクトルイメージ形式です.SVGSはビットマップよりも軽いです、そして、彼らはスケール(例えばアイコン、イラストなど)をシャープにするイメージをつくるのに用いられます.
SVGの構文
💡SVG usually consists of a variety of elements e.g. <path>, </style>,<circle> etc. nested inside an svg element. The inner elements enclosed inside the svg tags are used to describe specific properties of the SVG whereas the outer svg element which is normally referred to as the root element basically instantiate and terminate an instance of SVG.The root element may contain attributes like viewBox, class, id, fill, height, width and xmlns. The id and class attributes are usually used to extend functionality in Javascript or to style the svg element. The id attribute may also be used in XML specific configurations like the setting up of links. The xmlns attribute is a XML namespace, it is used by the browser to determine how to render the SVG.
VUEJSにおけるSVGSの使用
vue cliを使ってプロジェクトをブートします.
要件
vue create <project-name>
置換<project-name>
あなたのプロジェクトのユニークな名前で.私のプロジェクトはsvg-tutorial
.Default ([Vue 2] babel, eslint)
. メッセージを開始するには便利なコマンドが含まれて!
src
SVGに関連した我々の仕事の大部分がそのフォルダに制限されるので、フォルダ.. ├── babel.config.js ├── node_modules ├── package.json ├── package-lock.json ├── public ├── README.md └── src ├── App.vue ├── assets │ └── logo.png ├── components │ └── HelloWorld.vue └── main.js
- I have created a sample SVG to use in the interactive examples provided.. You may go ahead and download it by clicking this link.
You may choose to rename the file, then proceed to add it to the root of the src/assets
folder, such that the structure of the src
folder is similar to the one below:
. ├── App.vue ├── assets │ ├── logo.png │ └── sample.svg ├── components │ └── HelloWorld.vue └── main.js
インラインSVG
When using this method you include the svg
directly in your markup. It's important to mention that when the svg
is directly embedded in the document or template as in the case of Vuejs ,there is no need to include the xlmns
attribute.
The pros of using this method are that you can use CSS to style your svg
and use Javascript to extend its functionality just as you would a normal HTML element.
The main drawback of this method is that if your svg
is large or when you have a lot of svg
your template, your template becomes cluttered. In that case it would be better incorporate SVGs as standalone files as it is explained in the next section.
外部SVG
To embed an external SVG in your Vuejs template you use the methods listed below and always ensure that the xlmns
attribute is included in the root element of the SVG otherwise none of the methods provided will work!
💡
xmlns="http://www.w3.org/2000/svg"
( I ) VEVEsテンプレートに外部SVGをイメージ要素として埋め込む注意:置換
<file-name>
あなたが上記のSVGリソースに与えたユニークな名前で.このメソッドを使用すると、
svg
実際にはドキュメントには存在しませんが、<img>
要素.だから、ほとんどの場合は、通常のイメージとしてそれを操作することができます.このメソッドのconsは、スタイルの制限とSVGSの機能拡張を含みます、そして、あなたがあなたのテンプレートの多くのSVGを扱っているならば、それはイメージ要素でそれらを包装し続けるのはかなり面倒です.
( II )使用
SVG Loaders
Vuejsは、資産バンドルとしてWebpackを使用します.Webpackは、それを扱うファイルタイプごとにローダを使用します.一般的なファイルタイプのローダは通常、VUE CLIを使用してプロジェクトをブートするたびにあらかじめ設定されています.つまり、これらのファイルタイプをインポートするときに自動的に読み込むことができるということです.残念ながら.svg
ローダは事前に設定されていないので、それらをダウンロードする必要が来るnpm 手動で設定します.SVG VuejsローダであるNPMで利用できる多くのモジュールがあります.この例では、vue-svg-loader . セットアップされた指示は、インストールされたことがない人々のために提供されるリンクでリストされます、そして、UVEJsでモジュールを構成することは以下の指示に従います:
以下の指示はVue CLIでブートストラップされたプロジェクトに適しています.
npm i -D vue-svg-loader vue-template-compiler
vue.config.js
以下のコードをファイルとコピーしてペーストしますmodule.exports = {
chainWebpack: (config) => {
const svgRule = config.module.rule('svg');
svgRule.uses.clear();
svgRule
.use('babel-loader')
.loader('babel-loader')
.end()
.use('vue-svg-loader')
.loader('vue-svg-loader');
},
};
変更を保存することを確認します.おめでとう!🎊
あなたは正常にインストールされ、設定の設定
vue-svg-loader
モジュールです.では、どのように使うのかを学びましょう
vue-svg-loader
vvejsでSVGを埋め込むモジュール.App.vue
ファイル名src
ディレクトリ.App.vue
, 変更を保存します.svg
私は、スタンドアロンファイルとして存在していますsample.svg
, それで、あなたがあなたの名前をつけたものに従います.上記のコードを微調整する必要があります.http://localhost:8080/
埋め込みSVGを使用してVuejsアプリを表示するには🚀.npm run serve
このメソッドの使用方法は以下の通りです:参考文献
What are Scalable Vector Graphics (SVG) & how are they special? | Web Demystified Episode 4
Reference
この問題について(簡単であるVUEJSにおけるSVGSの使用), 我々は、より多くの情報をここで見つけました https://dev.to/jacqueline/using-svgs-in-vuejs-made-simple-2e1aテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol