<template><div id="my-component"><DemoComponent /></div></template><script>import DemoComponent from '../components/DemoComponent'export default {name: 'MyComponent',components: {DemoComponent},mixins: [],props: {},data () {return {}},computed: {},watch: {}created () {},mounted () {},destroyed () {},methods: {},}</script><style lang="scss" scoped>#my-component {}</style>
组件的 **data** 必须是一个函数。
// In a .vue fileexport default {data () {return {foo: 'bar'}}}
props 定义应该尽量详细。
export default {props: {status: {type: String,required: true,validator: function (value) {return ['syncing','synced','version-conflict','error'].indexOf(value) !== -1}}}}
应该把复杂计算属性分割为尽可能多的更简单的属性。 小的、专注的计算属性减少了信息使用时的假设性限制,所以需求变更时也用不着那么多重构了。
// badcomputed: {price: function () {var basePrice = this.manufactureCost / (1 - this.profitMargin)return (basePrice -basePrice * (this.discountPercent || 0))}}// goodcomputed: {basePrice: function () {return this.manufactureCost / (1 - this.profitMargin)},discount: function () {return this.basePrice * (this.discountPercent || 0)},finalPrice: function () {return this.basePrice - this.discount}}
v-for 设置键值在组件上必须用 **key** 搭配 **v-for**,以便维护内部组件及其子树的状态。甚至在元素上维护可预测的行为,比如动画中的对象固化 (object constancy)。
<ul><liv-for="todo in todos":key="todo.id">{{ todo.text }}</li></ul>
v-if 和 v-for 互斥永远不要把 **v-if** 和 **v-for** 同时用在同一个元素上。
<!-- bad:控制台报错 --><ul><liv-for="user in users"v-if="shouldShowUsers":key="user.id">{{ user.name }}</li></ul>
一般我们在两种常见的情况下会倾向于这样做:
v-for="user in users" v-if="user.isActive")。在这种情形下,请将 users 替换为一个计算属性 (比如 activeUsers),让其返回过滤后的列表。
computed: {activeUsers: function () {return this.users.filter((user) => {return user.isActive})}}
<ul><liv-for="user in activeUsers":key="user.id">{{ user.name }}</li></ul>
v-for="user in users" v-if="shouldShowUsers")。这种情形下,请将 v-if 移动至容器元素上 (比如 ul, ol)。
<!-- bad --><ul><liv-for="user in users"v-if="shouldShowUsers":key="user.id">{{ user.name }}</li></ul><!-- good --><ul v-if="shouldShowUsers"><liv-for="user in users":key="user.id">{{ user.name }}</li></ul>
多个 attribute 的元素应该分多行撰写,每个属性一行。
<!-- bad --><img src="https://vuejs.org/images/logo.png" alt="Vue Logo"><MyComponent foo="a" bar="b" baz="c"/>
<!-- good --><imgsrc="https://vuejs.org/images/logo.png"alt="Vue Logo"><MyComponentfoo="a"bar="b"baz="c"/>
组件模板应该只包含简单的表达式,复杂的表达式则应该重构为计算属性或方法。
复杂表达式会让你的模板变得不那么声明式。我们应该尽量描述应该出现的是什么,而非如何计算那个值。而且计算属性和方法使得代码可以重用。
// bad{{fullName.split(' ').map((word) => {return word[0].toUpperCase() + word.slice(1)}).join(' ')}}
更好的做法:
<!-- 在模板中 -->{{ normalizedFullName }}
// 复杂表达式已经移入一个计算属性computed: {normalizedFullName: function () {return this.fullName.split(' ').map(function (word) {return word[0].toUpperCase() + word.slice(1)}).join(' ')}}
非空 HTML 特性值应该始终带双引号。
<!-- bad --><input type=text><AppSidebar :style={width:sidebarWidth+'px'}>
<!-- good --><input type="text"><AppSidebar :style="{ width: sidebarWidth + 'px' }">
**:** 表示 **v-bind:****@** 表示 **v-on:****#** 表示 **v-slot:**
<input:value="newTodoText":placeholder="newTodoInstructions"><input@input="onInput"@focus="onFocus"><template #header><h1>Here might be a page title</h1></template><template #footer><p>Here's some contact info</p></template>
HTML5 文件模板:
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>HTML5标准模版</title></head><body></body></html>
移动端:
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"><meta name="format-detection" content="telephone=no"><title>移动端HTML模版</title><!-- S DNS预解析 --><link rel="dns-prefetch" href=""><!-- E DNS预解析 --><!-- S 线上样式页面片,开发请直接取消注释引用 --><!-- #include virtual="" --><!-- E 线上样式页面片 --><!-- S 本地调试,根据开发模式选择调试方式,请开发删除 --><link rel="stylesheet" href="css/index.css"><!-- /本地调试方式 --><link rel="stylesheet" href="http://srcPath/index.css"><!-- /开发机调试方式 --><!-- E 本地调试 --></head><body></body></html>
PC 端:
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="keywords" content="your keywords"><meta name="description" content="your description"><meta name="author" content="author,email address"><meta name="robots" content="index,follow"><meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"><meta name="renderer" content="ie-stand"><title>PC端HTML模版</title><!-- S DNS预解析 --><link rel="dns-prefetch" href=""><!-- E DNS预解析 --><!-- S 线上样式页面片,开发请直接取消注释引用 --><!-- #include virtual="" --><!-- E 线上样式页面片 --><!-- S 本地调试,根据开发模式选择调试方式,请开发删除 --><link rel="stylesheet" href="css/index.css"><!-- /本地调试方式 --><link rel="stylesheet" href="http://srcPath/index.css"><!-- /开发机调试方式 --><!-- E 本地调试 --></head><body></body></html>
HTML 元素共有以下5种:
为了能让浏览器更好的解析代码以及能让代码具有更好的可读性,有如下约定:
<!-- good --><div><h1>我是h1标题</h1><p>我是一段文字,我有始有终,浏览器能正确解析</p></div><br data-tomark-pass><!-- bad --><div><h1>我是h1标题</h1><p>我是一段文字,我有始无终,浏览器亦能正确解析</div><br/>
元素嵌套规范,每个块状元素独立一行,内联元素可选。
<!-- good --><div><h1></h1><p></p></div><p><span></span><span></span></p><!-- bad --><div><h1></h1><p></p></div><p><span></span><span></span></p>
段落元素与标题元素只能嵌套内联元素。
<!-- good --><h1><span></span></h1><p><span></span><span></span></p><!-- bad --><h1><div></div></h1><p><div></div><div></div></p>
样式文件必须写上 @charset 规则,并且一定要在样式文件的第一行首个字符位置开始写,编码名用 “UTF-8”。
@charset "UTF-8";.jdc {}
/* @charset规则不在文件首行首个字符开始 */@charset "UTF-8";.jdc {}/* @charset规则没有用小写 */@CHARSET "UTF-8";.jdc {}/* 无@charset规则 */.jdc {}
样式书写一般有两种:一种是紧凑格式 (Compact),一种是展开格式(Expanded)。
.jdc {display: block;width: 50px;}
.jdc { display: block; width: 50px;}
样式选择器,属性名,属性值关键字全部使用小写字母书写,属性字符串允许使用大小写。
.jdc {display: block;}
.JDC {DISPLAY: BLOCK;}
.jdc {width: 100%;}
.jdc{width:100%;}
.jdc {box-shadow: 1px 1px 1px #333, 2px 2px 2px #ccc;}
.jdc {box-shadow: 1px 1px 1px #333,2px 2px 2px #ccc;}
.jdc, .jdc_logo, .jdc_hd {color: #ff0;}.nav{color: #fff;}
.jdc, .jdc_logo, .jdc_hd {color: #ff0;}.nav{color: #fff;}
rgb() rgba() hsl() hsla() rect() 中不需有空格,且取值不要带有不必要的 0。
.jdc {color: rgba(255,255,255,.5);}
.jdc {color: rgba( 255, 255, 255, 0.5 );}
.jdc {color: #fff;}
.jdc {color: #ffffff;}
0 指明单位。
.jdc {margin: 0 10px;}
.jdc {margin: 0px 10px;}
CSS 属性值需要用到引号时,统一使用单引号。
.jdc {font-family: 'Hiragino Sans GB';}
.jdc {font-family: "Hiragino Sans GB";}
建议遵循以下顺序:
.jdc {display: block;position: relative;float: left;width: 100px;height: 100px;margin: 0 10px;padding: 20px 0;font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;color: #333;background: rgba(0,0,0,.5);-webkit-border-radius: 10px;-moz-border-radius: 10px;-o-border-radius: 10px;-ms-border-radius: 10px;border-radius: 10px;}
CSS3 浏览器私有前缀在前,标准前缀在后。
.jdc {-webkit-border-radius: 10px;-moz-border-radius: 10px;-o-border-radius: 10px;-ms-border-radius: 10px;border-radius: 10px;}
在单行代码块中使用空格。
function foo () {return true}if (foo) {bar = 0}
function foo () { return true }if (foo) { bar = 0 }
在编程过程中,大括号风格与缩进风格紧密联系,用来描述大括号相对代码块位置的方法有很多。在 JavaScript 中,主要有三种风格,如下:
if (foo) {bar()} else {baz()}
if (foo) {bar()}else {baz()}
if (foo){bar()}else{baz()}
var foo = 1, bar = 2
var foo = 1,bar = 2var foo = 1 , bar = 2var foo = 1 ,bar = 2
var obj = { 'foo': 'haha' }
var obj = { 'foo' : 'haha' }
if (a) {b()}function a () {}
if (a){b()}function a (){}
function func (x) {// ...}
function func(x) {// ...}
fn()
fn ()fn()
var sum = 1 + 2
var sum = 1+2
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号