Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<div class='parent'>
  <tagged-input v-model="taggedInput"
        :handleInput="handleInput"></tagged-input>
  <p>这是希望获取的子组件数据:{{taggedInput}}</p>
</div>

              
            
!

CSS

              
                .input-wrapper {
        padding: 0 7px;
        width: 40% ;
        vertical-align: middle;
        line-height: 1.5;
        font-size: 12px;
        color: #666;
        background-color: #fff;
        background-image: none;
        border-radius: 4px;
        border: 1px solid #d9d9d9;
      &:focus {
          border-color: #40a5ed;
          box-shadow: 0 0 0 2px rgba(16,142,233,0.2);
      }
      &:hover{
        border-color: #40a5ed;
      };
      .tag{
        /*float: left;*/
        padding: 0 8px;
        margin: 4px 8px 4px 0;
        border-radius: 4px;
        border: 1px solid #e9e9e9;
        background:#f7f7f7;
        font-size:12px;
        line-height: 20px;
        display: inline-block;
        &:focus{
          border-color: #E60D0D;
        }
        .iconfont{
          font-size: 12px;
          cursor: pointer;
        }


      }
}
    .input {
        outline: none;
        border: none;
        appearance: none;
        display: inline-block;
        width: 100%;
        min-height: 28px;
        cursor: text;
    }


              
            
!

JS

              
                Vue.component("tagged-input", {
  template: `
  <div class='child'>
  <div class="input-wrapper clearfix">
     <li :tabindex="index" :class="['tag left',{'focus':index===deleteIndex}]" v-for="(tag,index) in tagList" :key="tag" ref="tags">
     <span>{{tag}}</span>
     <i class="iconfont icon-delete" @click="deleteTag(index)"></i>
          </li><!-- </ul> -->
        <div class="left input-box">
          <input type="text" class="input" :placeholder="placeholder" v-model="currentValue"
            @keyup.enter="onInputEnter"
            @input="onInput"
            @keyup.8.prevent.stop="onBackspacePress"></input>
        </div>
      </div>
    </div>
</div>


`,
  props: {
    value: [String, Number],
    placeholder: {
      type: [String, Number],
      default: ''
    },
    tagged: {
      type: Boolean,
      default: true
    },
    handleInput: Function
  },
  data() {
    return {
      currentValue: this.value,
      deleteIndex: -1,
      tagList: ['test1', 'test2']
    }
  },
  watch:{
    tagList(val){
      this.$emit('input',val.join(","));
    }
  },
  methods: {
    onInputEnter() {
      this.tagList.push(this.currentValue);
      this.currentValue = "";
      // this.$emit("input",this.currentValue);
    },
    /*
    input框有值则清空,第二次按键focus上一个tag。再次按则删除tag.
    */
    onBackspacePress(event) {

      if (event.target.value)
        this.currentValue = "";
      else {
        if (this.tagList.length > 0)
          this.deleteTag(this.tagList.length - 1);
      }
      return false;

    },
    deleteTag(index) {
      this.tagList.splice(index, 1);
    },
    onInput(event) {
      this.currentValue = event.target.value;
      console.log(" child :in tagged ",this.currentValue)
      // this.$emit("input",this.currentValue);
      if (this.handleInput)
        this.handleInput(this.currentValue);
    },

  }

})

let parent = new Vue({
  el: '.parent',
  data: {
    taggedInput: ''
  },
  watch:{
   "taggedInput"(val){
     console.log("parent watch input: ",val);
   } 
  },
  methods: {
    handleInput(val) {
      console.log(this.input);
    }
  }
})


/*
backspace回退问题:http://stackoverflow.com/questions/6587525/javascript-backspace-by-default-problem-going-back-a-page
*/
              
            
!
999px

Console