- Full-Stack Vue.js 2 and Laravel 5
- Anthony Gore
- 127字
- 2021-07-02 19:57:20
Style binding (continued)
Coming back to our header image, we can use thev-binddirective with thestyleargumentto bind a value to thestyleattribute.
index.html:
<p class="header-img" v-bind:style="headerImageStyle"></p>
headerImageStyleis an expression that evaluates to a CSS rule that sets the background image to the correct URL. It sounds very confusing, but when you see it working, it will be quite clear.
Let's now createheaderImageStyleas a data property. When binding to a style attribute, you can use an object where the properties and values are equivalent to the CSS properties and values.
app.js:
data: { ... headerImageStyle: { 'background-image': 'url(sample/header.jpg)' } },
Save the code, refresh the page, and the header image will be shown:

Figure 2.6. Page including header image
Inspect the page with your browser Dev Tools and notice how thev-binddirective has evaluated:
<p class="header-img" style="background-image: url('sample/header.jpg');"></p>
推薦閱讀
- Mastering Ext JS(Second Edition)
- 計(jì)算思維與算法入門
- Node.js 10實(shí)戰(zhàn)
- Learning PostgreSQL
- 自己動(dòng)手寫Java虛擬機(jī)
- Visual C++實(shí)例精通
- 趣學(xué)Python算法100例
- Python機(jī)器學(xué)習(xí)編程與實(shí)戰(zhàn)
- TradeStation交易應(yīng)用實(shí)踐:量化方法構(gòu)建贏家策略(原書第2版)
- Learning Salesforce Einstein
- NoSQL數(shù)據(jù)庫原理
- RubyMotion iOS Develoment Essentials
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級(jí)賬本看區(qū)塊鏈架構(gòu)設(shè)計(jì)
- Distributed Computing in Java 9
- Mastering HTML5 Forms