当前位置:   article > 正文

用 Vue+ TypeScript 开发页面_typescript写界面

typescript写界面

 

1.创建项目

  • vue create my-ts ,my-ts就是所创建的项目的名称
  • vue create my-ts
  • 选择 Manually select features 回车确认
  • 选择typescript  vuex   router  babel  css  pre-processors  Linter/Formatter 空格键选中和取消,回车确认
  • linter配置选择  ESLint+Prettier

2.开始编码

import { Vue, Component, Prop, Provide, Inject, Model, Watch, Emit, Mixins } from 'vue-property-decorator'

(1)Vue 实际上就是 Vue 本身,用来继承vue相关属性和函数 

(2)@Component 声明成一个vue的组件实例,不使用的话,就不能得到一个vue组件

 

  1. import { Component, Vue } from "vue-property-decorator";
  2. // 不需要定义额外内容
  3. @Component
  4. export default class MyComponent extends Vue {}

 

  1. import { Component, Vue } from "vue-property-decorator";
  2. import bank102 from "@/assets/image/bank-list/102_GongShang.png";
  3. import bank103 from "@/assets/image/bank-list/103_NongYe.png";
  4. import bank104 from "@/assets/image/bank-list/104_ZhongGuo.png";
  5. // 需要定义一些相关内容
  6. @Component({
  7. /* 这里和js版本编写的 vue 组件内容相同,
  8. * 凡是不能在ts里面完成的都可以在这里完成
  9. * 最终会被合并到一个实例中。
  10. */
  11. components: {
  12. componentsA,
  13. componentsB
  14. },
  15. })
  16. export default class MyComponent extends Vue {
  17. // data
  18. name: string = "jim";
  19. age: number = 12;
  20. bankList: object[] = [];
  21. showFlag: boolean = false;
  22. imgSrc: object = {
  23. bank102,
  24. bank103,
  25. bank104
  26. };
  27. certTypeList: { key: string; value: string }[] = [
  28. { key: "0", value: "身份证" },
  29. { key: "1", value: "户口簿" },
  30. { key: "2", value: "护照" }
  31. ];
  32. // methods
  33. getBankList(){
  34. }
  35. mounted(){
  36. // TODO
  37. }
  38. }

(3)@Prop 由标签属性注入,获取对应标签属性值,可配置具体prop内容

  1. import { Vue, Component, Prop } from 'vue-property-decorator'
  2. @Component
  3. export default class YourComponent extends Vue {
  4. @Prop(Number) readonly propA: number | undefined
  5. @Prop({ default: 'default value' }) readonly propB!: string
  6. @Prop([String, Boolean]) readonly propC: string | boolean | undefined
  7. }

 相当于

  1. export default {
  2. props: {
  3. propA: {
  4. type: Number
  5. },
  6. propB: {
  7. default: 'default value'
  8. },
  9. propC: {
  10. type: [String, Boolean]
  11. }
  12. }
  13. }

(4)@Provide 向任意层级的子组件提供可访问的属性

(5)@Inject 获取父级由Provide提供的属性

  1. import { Component, Inject, Provide, Vue } from 'vue-property-decorator'
  2. const symbol = Symbol('baz')
  3. @Component
  4. export class MyComponent extends Vue {
  5. @Inject() readonly foo!: string
  6. @Inject('bar') readonly bar!: string
  7. @Inject({ from: 'optional', default: 'default' }) readonly optional!: string
  8. @Inject(symbol) readonly baz!: string
  9. @Provide() foo = 'foo'
  10. @Provide('bar') baz = 'bar'
  11. }

相当于

  1. const symbol = Symbol('baz')
  2. export const MyComponent = Vue.extend({
  3. inject: {
  4. foo: 'foo',
  5. bar: 'bar',
  6. optional: { from: 'optional', default: 'default' },
  7. [symbol]: symbol
  8. },
  9. data() {
  10. return {
  11. foo: 'foo',
  12. baz: 'bar'
  13. }
  14. },
  15. provide() {
  16. return {
  17. foo: this.foo,
  18. bar: this.baz
  19. }
  20. }
  21. })

(6)@Watch 观察某个属性更新

  1. import { Vue, Component, Watch } from 'vue-property-decorator'
  2. @Component
  3. export default class YourComponent extends Vue {
  4. @Watch('child')
  5. onChildChanged(val: string, oldVal: string) {}
  6. @Watch('person', { immediate: true, deep: true })
  7. onPersonChanged1(val: Person, oldVal: Person) {}
  8. @Watch('person')
  9. onPersonChanged2(val: Person, oldVal: Person) {}
  10. }

相当于

  1. export default {
  2. watch: {
  3. child: [
  4. {
  5. handler: 'onChildChanged',
  6. immediate: false,
  7. deep: false
  8. }
  9. ],
  10. person: [
  11. {
  12. handler: 'onPersonChanged1',
  13. immediate: true,
  14. deep: true
  15. },
  16. {
  17. handler: 'onPersonChanged2',
  18. immediate: false,
  19. deep: false
  20. }
  21. ]
  22. },
  23. methods: {
  24. onChildChanged(val, oldVal) {},
  25. onPersonChanged1(val, oldVal) {},
  26. onPersonChanged2(val, oldVal) {}
  27. }
  28. }

(7)@Model 是v-model的装饰器,当自定义组件想使用v-model时,可以使用这种方式,配合emit可以双向传递属性值

  1. import { Vue, Component, Model } from 'vue-property-decorator'
  2. @Component
  3. export default class YourComponent extends Vue {
  4. @Model('change', { type: Boolean }) readonly checked!: boolean
  5. }

相当于

  1. export default {
  2. model: {
  3. prop: 'checked',
  4. event: 'change'
  5. },
  6. props: {
  7. checked: {
  8. type: Boolean
  9. }
  10. }
  11. }

(8)@Emit this.$emit 的装饰器,如果没有指定名称,默认使用函数名称。有返回值时,使用返回值,没有不使用

  1. import { Vue, Component, Emit } from 'vue-property-decorator'
  2. @Component
  3. export default class YourComponent extends Vue {
  4. count = 0
  5. @Emit()
  6. addToCount(n: number) {
  7. this.count += n
  8. }
  9. @Emit('reset')
  10. resetCount() {
  11. this.count = 0
  12. }
  13. @Emit()
  14. returnValue() {
  15. return 10
  16. }
  17. @Emit()
  18. onInputChange(e) {
  19. return e.target.value
  20. }
  21. @Emit()
  22. promise() {
  23. return new Promise(resolve => {
  24. setTimeout(() => {
  25. resolve(20)
  26. }, 0)
  27. })
  28. }
  29. }

相当于

  1. export default {
  2. data() {
  3. return {
  4. count: 0
  5. }
  6. },
  7. methods: {
  8. addToCount(n) {
  9. this.count += n
  10. this.$emit('add-to-count', n)
  11. },
  12. resetCount() {
  13. this.count = 0
  14. this.$emit('reset')
  15. },
  16. returnValue() {
  17. this.$emit('return-value', 10)
  18. },
  19. onInputChange(e) {
  20. this.$emit('on-input-change', e.target.value, e)
  21. },
  22. promise() {
  23. const promise = new Promise(resolve => {
  24. setTimeout(() => {
  25. resolve(20)
  26. }, 0)
  27. })
  28. promise.then(value => {
  29. this.$emit('promise', value)
  30. })
  31. }
  32. }
  33. }

9Mixins

  1. // AMixin.ts
  2. @Component
  3. export default class AMixin extends Vue {
  4. protected myname = "A";
  5. created(){}
  6. getMyName(){ console.log(this.myname) }
  7. }

 

  1. // BMixin.ts
  2. @Component
  3. export default class BMixin extends Vue {
  4. protected myname = "B";
  5. created(){}
  6. getMyName(){ console.log(this.myname) }
  7. }

页面中使用

  1. @Component
  2. class MyComponent extends Mixins(AMixin,BMixin) {
  3. mounted(){
  4. this.getMyName() // B
  5. }
  6. }

10computed 对于计算属性computed ,可以用get代替

  1. import { Vue, Component } from "vue-property-decorator";
  2. @Component
  3. class MyComponent extends Vue {
  4. private myname = "jim";
  5. get msg(){
  6. return "您好!" + this.myname
  7. }
  8. }

相当于

  1. export default{
  2. data() {
  3. return{
  4. myname: "jim"
  5. }
  6. },
  7. computed:{
  8. msg(){
  9. return "您好!" + this.myname;
  10. }
  11. }
  12. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/797231
推荐阅读
  

闽ICP备14008679号