初学者与vue.js树视图

时间:2015-09-29 11:26:24

标签: javascript html treeview vue.js

学习JS并尝试从Vue.js中找出树视图。

示例位于Vue网站上:Tree view on Vue site

我所做的是创建一个HTML文档,其中包含根据JSFiddle的HTML代码:

<!DOCTYPE html>
<html>

<head>

<style>
body {
  font-family: Menlo, Consolas, monospace;
  color: #444;
}
.item {
cursor: pointer;
}
.bold {
  font-weight: bold;
}
ul {
  padding-left: 1em;
  line-height: 1.5em;
  list-style-type: dot;
}
</style>
</head>

<body>
<script type="text/x-template" id="template">
  <div v-class="bold: isFolder"
    v-on="click: toggle, dblclick: changeType">
    {{model.name}}
    <span v-if="isFolder">[{{open ? '-' : '+'}}]</span>
  </div> 
  <ul v-show="open" v-if="isFolder">
    <li class="item"
      v-repeat="model: model.children"
      v-component="item">
    </li>
    <li v-on="click: addChild">+</li>
  </ul>
</script>
<script src="JS/app.js"></script>
<script src="JS/vue.min.js"></script>
<p>(You can double click on an item to turn it into a folder.)</p>

<!-- the demo root element -->
<ul id="demo">
  <li class="item"
    v-component="item"
    v-with="model: treeData">
  </li>
</ul>



</body>
</html>

然后我将Javascript添加到一个单独的app.js文件中,并将其放在与名为JS的html文件相同的目录中的文件夹中。

我还将vue.min.js放在该文件夹中,但代码根本不起作用。 看起来脚本没有像CSS一样运行,其他一切都显示OK。 我可能在指向正确的js文件或留下一些东西方面犯了一个相当基本的错误但是语法还没有从在线演示工作中改变,所以我怀疑它是不是。<\ n / p>

JS:

// demo data
var data = {
  name: 'My Tree',
  children: [
    { name: 'hello' },
    { name: 'wat' },
    {
      name: 'child folder',
      children: [
        {
          name: 'child folder',
          children: [
            { name: 'hello' },
            { name: 'wat' }
          ]
        },
        { name: 'hello' },
        { name: 'wat' },
        {
          name: 'child folder',
          children: [
            { name: 'hello' },
            { name: 'wat' }
          ]
        }
      ]
    }
  ]
}

// define the item component
Vue.component('item', {
  template: '#template',
  data: function () {
    return {
      open: false
    }
  },
  computed: {
    isFolder: function () {
      return this.model.children &&
        this.model.children.length
    }
  },
  methods: {
    toggle: function () {
      if (this.isFolder) {
        this.open = !this.open
      }
    },
    changeType: function () {
      if (!this.isFolder) {
        this.model.$add('children', [])
        this.addChild()
        this.open = true
      }
    },
    addChild: function () {
      this.model.children.push({
        name: 'new stuff'
      })
    }
  }
})

// boot up the demo
var demo = new Vue({
  el: '#demo',
  data: {
    treeData: data
  }
})

如果有人对我的行为有任何疑问,请告诉我。

所有浏览器(Safari,Firefox,Chrome)上都存在问题 - &gt;我相当肯定这是一个高级问题,因为上面链接的JSFiddle页面和示例页面都显示正确,我只是将代码复制+粘贴到html和js文件中,除了下载和引用vue.min.js

欢迎所有帮助和建议!

中号

编辑:

在Orland的回答下面,我将所有代码包含在一个文件中,如下所示:

<!DOCTYPE html>
<html>

<head>
<style>
body {
  font-family: Menlo, Consolas, monospace;
  color: #444;
}
.item {
  cursor: pointer;
}
.bold {
  font-weight: bold;
}
ul {
  padding-left: 1em;
  line-height: 1.5em;
  list-style-type: dot;
}
</style>
</head>

<body>
<script src="https://cdn.rawgit.com/yyx990803/vue/master/dist/vue.min.js"></script>
<!-- item template -->
<script type="text/x-template" id="item-template">
  <div v-class="bold: isFolder"
    v-on="click: toggle, dblclick: changeType">
    {{model.name}}
    <span v-if="isFolder">[{{open ? '-' : '+'}}]</span>
  </div>
  <ul v-show="open" v-if="isFolder">
    <li class="item"
      v-repeat="model: model.children"
      v-component="item">
    </li>
    <li v-on="click: addChild">+</li>
  </ul>
</script>

<p>(You can double click on an item to turn it into a folder.)</p>

<!-- the demo root element -->
<ul id="demo">
    <item model="{{ treeData }}"></item>
</ul>
<script>
    // demo data
    var data = {
        name: 'My Tree',
        children: [
            { name: 'wat' },
            {
                name: 'child folder',
                children: [
                    {
                        name: 'child folder',
                        children: [
                            { name: 'hello' },
                            { name: 'wat' }
                        ]
                    },
                    { name: 'hello' },
                    { name: 'wat' },
                    {
                        name: 'child folder',
                        children: [
                            { name: 'hello' },
                            { name: 'wat' }
                        ]
                    }
                ]
            }
        ]
    }

    // define the item component
    Vue.component('item', {
        template: '#item-template',
        props: ['model'],
        data: function () {
            return {
                open: false,
                model: {}
            }
        },
        computed: {
            isFolder: function () {
                return this.model.children &&
                        this.model.children.length
            }
        },
        methods: {
            toggle: function () {
                if (this.isFolder) {
                    this.open = !this.open
                }
            },
            changeType: function () {
                if (!this.isFolder) {
                    this.model.$add('children', [])
                    this.addChild()
                    this.open = true
                }
            },
            addChild: function () {
                this.model.children.push({
                    name: 'new stuff'
                })
            }
        }
    })

    // boot up the demo
    var demo = new Vue({
        el: '#demo',
        data: {
            treeData: data
        }
    })

</script>
</body>
</html>

这完美无缺,但我正在开发一个大部分离线运行的应用程序,所以我尝试将vue.min.js源更改为我拥有的本地vue.min.js并且它停止工作!!我做的改变是:

来自<script src="https://cdn.rawgit.com/yyx990803/vue/master/dist/vue.min.js"></script><script src="JS/vue.min.js"></script>

无法理解这一点但是假设我正在查找vue.min.js !!! ???

1 个答案:

答案 0 :(得分:1)

似乎即使是Vue JS网站上的原始代码段也无效。我更新了代码段以使其正常工作。

&#13;
&#13;
// demo data
var data = {
  name: 'My Tree',
  children: [
    { name: 'wat' },
    {
      name: 'child folder',
      children: [
        {
          name: 'child folder',
          children: [
            { name: 'hello' },
            { name: 'wat' }
          ]
        },
        { name: 'hello' },
        { name: 'wat' },
        {
          name: 'child folder',
          children: [
            { name: 'hello' },
            { name: 'wat' }
          ]
        }
      ]
    }
  ]
}

// define the item component
Vue.component('item', {
  template: '#item-template',
  props: ['model'],
  data: function () {
    return {
      open: false,
      model: {}
    }
  },
  computed: {
    isFolder: function () {
      return this.model.children &&
        this.model.children.length
    }
  },
  methods: {
    toggle: function () {
      if (this.isFolder) {
        this.open = !this.open
      }
    },
    changeType: function () {
      if (!this.isFolder) {
        this.model.$add('children', [])
        this.addChild()
        this.open = true
      }
    },
    addChild: function () {
      this.model.children.push({
        name: 'new stuff'
      })
    }
  }
})

// boot up the demo
var demo = new Vue({
  el: '#demo',
  data: {
    treeData: data
  }
})
&#13;
body {
  font-family: Menlo, Consolas, monospace;
  color: #444;
}
.item {
  cursor: pointer;
}
.bold {
  font-weight: bold;
}
ul {
  padding-left: 1em;
  line-height: 1.5em;
  list-style-type: dot;
}
&#13;
<script src="https://cdn.rawgit.com/yyx990803/vue/master/dist/vue.min.js"></script>
<!-- item template -->
<script type="text/x-template" id="item-template">
  <div v-class="bold: isFolder"
    v-on="click: toggle, dblclick: changeType">
    {{model.name}}
    <span v-if="isFolder">[{{open ? '-' : '+'}}]</span>
  </div>
  <ul v-show="open" v-if="isFolder">
    <li class="item"
      v-repeat="model: model.children"
      v-component="item">
    </li>
    <li v-on="click: addChild">+</li>
  </ul>
</script>

<p>(You can double click on an item to turn it into a folder.)</p>

<!-- the demo root element -->
<ul id="demo">
    <item model="{{ treeData }}"></item>
</ul>
&#13;
&#13;
&#13;