options-test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import Vue = require("../index");
  2. import { ComponentOptions, FunctionalComponentOptions } from "../index";
  3. interface Component extends Vue {
  4. a: number;
  5. }
  6. Vue.component('component', {
  7. data() {
  8. this.$mount
  9. this.a
  10. return {
  11. a: 1
  12. }
  13. },
  14. props: {
  15. size: Number,
  16. name: {
  17. type: String,
  18. default: 0,
  19. required: true,
  20. validator(value) {
  21. return value > 0;
  22. }
  23. }
  24. },
  25. propsData: {
  26. msg: "Hello"
  27. },
  28. computed: {
  29. aDouble(this: Component) {
  30. return this.a * 2;
  31. },
  32. aPlus: {
  33. get(this: Component) {
  34. return this.a + 1;
  35. },
  36. set(this: Component, v: number) {
  37. this.a = v - 1;
  38. },
  39. cache: false
  40. }
  41. },
  42. methods: {
  43. plus() {
  44. this.a++;
  45. }
  46. },
  47. watch: {
  48. 'a': function(val: number, oldVal: number) {
  49. console.log(`new: ${val}, old: ${oldVal}`);
  50. },
  51. 'b': 'someMethod',
  52. 'c': {
  53. handler(val, oldVal) {
  54. this.a = val
  55. },
  56. deep: true
  57. }
  58. },
  59. el: "#app",
  60. template: "<div>{{ message }}</div>",
  61. render(createElement) {
  62. return createElement("div", {
  63. attrs: {
  64. id: "foo"
  65. },
  66. props: {
  67. myProp: "bar"
  68. },
  69. domProps: {
  70. innerHTML: "baz"
  71. },
  72. on: {
  73. click: new Function
  74. },
  75. nativeOn: {
  76. click: new Function
  77. },
  78. class: {
  79. foo: true,
  80. bar: false
  81. },
  82. style: {
  83. color: 'red',
  84. fontSize: '14px'
  85. },
  86. key: 'myKey',
  87. ref: 'myRef'
  88. }, [
  89. createElement(),
  90. createElement("div", "message"),
  91. createElement(Vue.component("component")),
  92. createElement({} as ComponentOptions<Vue>),
  93. createElement({ functional: true }),
  94. createElement(() => Vue.component("component")),
  95. createElement(() => ( {} as ComponentOptions<Vue> )),
  96. createElement(() => {
  97. return new Promise((resolve) => {
  98. resolve({} as ComponentOptions<Vue>);
  99. })
  100. }),
  101. createElement((resolve, reject) => {
  102. resolve({} as ComponentOptions<Vue>);
  103. reject();
  104. }),
  105. "message",
  106. [createElement("div", "message")]
  107. ]);
  108. },
  109. staticRenderFns: [],
  110. beforeCreate() {
  111. this.a = 1;
  112. },
  113. created() {},
  114. beforeDestroy() {},
  115. destroyed() {},
  116. beforeMount() {},
  117. mounted() {},
  118. beforeUpdate() {},
  119. updated() {},
  120. activated() {},
  121. deactivated() {},
  122. directives: {
  123. a: {
  124. bind() {},
  125. inserted() {},
  126. update() {},
  127. componentMounted() {},
  128. unbind() {}
  129. },
  130. b(el, binding, vnode, oldVnode) {
  131. el.textContent;
  132. binding.name;
  133. binding.value;
  134. binding.oldValue;
  135. binding.expression;
  136. binding.arg;
  137. binding.modifiers["modifier"];
  138. }
  139. },
  140. components: {
  141. a: Vue.component(""),
  142. b: {} as ComponentOptions<Vue>
  143. },
  144. transitions: {},
  145. filters: {
  146. double(value: number) {
  147. return value * 2;
  148. }
  149. },
  150. parent: new Vue,
  151. mixins: [Vue.component(""), ({} as ComponentOptions<Vue>)],
  152. name: "Component",
  153. extends: {} as ComponentOptions<Vue>,
  154. delimiters: ["${", "}"]
  155. } as ComponentOptions<Component>);
  156. Vue.component('component-with-scoped-slot', {
  157. render (h) {
  158. interface ScopedSlotProps {
  159. msg: string
  160. }
  161. return h('div', [
  162. h('child', [
  163. // default scoped slot as children
  164. (props: ScopedSlotProps) => [h('span', [props.msg])]
  165. ]),
  166. h('child', {
  167. scopedSlots: {
  168. // named scoped slot as vnode data
  169. item: (props: ScopedSlotProps) => [h('span', [props.msg])]
  170. }
  171. })
  172. ])
  173. },
  174. components: {
  175. child: {
  176. render (h) {
  177. return h('div', [
  178. this.$scopedSlots['default']({ msg: 'hi' }),
  179. this.$scopedSlots['item']({ msg: 'hello' })
  180. ])
  181. }
  182. } as ComponentOptions<Vue>
  183. }
  184. } as ComponentOptions<Vue>)
  185. Vue.component('functional-component', {
  186. props: ['prop'],
  187. functional: true,
  188. render(createElement, context) {
  189. context.props;
  190. context.children;
  191. context.slots();
  192. context.data;
  193. context.parent;
  194. return createElement("div", {}, context.children);
  195. }
  196. } as FunctionalComponentOptions);
  197. Vue.component("async-component", (resolve, reject) => {
  198. setTimeout(() => {
  199. resolve(Vue.component("component"));
  200. }, 0);
  201. return new Promise((resolve) => {
  202. resolve({ functional: true } as FunctionalComponentOptions);
  203. })
  204. });