上面的例子我们的组件里面data的属性是固定的,那么我有什么办法可以修改firstname 和 secondname的值呢?组件一般不能写的太死,组件的内容一般需要外边传
    所以我们需要定义一些属性
    也就是说我在使用组件的时候怎么传递变量给组件,并被组件接收呢?
    1.组件中设置props用来接受外部变量
    2.使用组件的时候,传入变量

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title></title>
    6. </head>
    7. <body>
    8. <script type="text/javascript" src="https://unpkg.com/vue/dist/vue.js"></script>
    9. <div id="app">
    10. </div>
    11. <script>
    12. //1.创建一个组件
    13. var MyCompose = {
    14. props:["firstname","secondname"],
    15. data() {
    16. return{
    17. title : "hello",
    18. }
    19. },
    20. //method和computed都可以保留
    21. methods: {
    22. fullname() {
    23. return this.firstname + this.secondname
    24. }
    25. },
    26. template: `
    27. <h3> {{ firstname }} {{ secondname }} {{ fullname() }} </h3>
    28. `,
    29. };
    30. //2.全局注册组件,注意最好使用驼峰写法
    31. // Vue.component("MyComp",MyCompose);
    32. //3.使用组件
    33. var vm = new Vue({
    34. el:"#app",
    35. //4.局部注册组件
    36. components: {
    37. MyCompose,
    38. },
    39. template:`
    40. <MyCompose firstname="a" secondname="b" ></MyCompose>
    41. `,
    42. })
    43. </script>
    44. </body>
    45. </html>