美文网首页
iframe与vue组件的交互

iframe与vue组件的交互

作者: 罗不错 | 来源:发表于2020-12-01 15:30 被阅读0次

    https://www.npmjs.com/package/postmate

    child iframe

    <script>
    import Postmate from 'postmate'
    export default {
      mounted() {
        const handshake = new Postmate.Model({
          haha: '123',
          height: () => 500,
          abc(p) {
            console.log(p)
          },
        })
        // When parent <-> child handshake is complete, events may be emitted to the parent
        handshake.then((child) => {
          child.emit('some-event', 'Hello, World!')
          console.log(child)
        })
      },
    }
    </script>
    

    parent

    <script>
    import Postmate from 'postmate'
    export default {
      data() {
        return {
          $child: null,
          abc: 'haha',
        }
      },
      mounted() {
        const handshake = new Postmate({
          container: document.getElementById('some-div'),
          url: 'http://localhost:8081/#/g',
          name: 'my-iframe-name',
          model: {
            haha: '456',
          },
          classListArray: ['myClass', 'myClass1'],
        })
    
        handshake.then((child) => {
          this.$child = child
          child.get('height').then((height) => {
            child.frame.style.height = `${height}px`
          })
          child.on('some-event', (data) => console.log(data)) // Logs "Hello, World!"
        })
      },
      methods: {
        sendEvent() {
          this.$child.call('abc', 'haha')
        },
      },
    }
    </script>
    

    相关文章

      网友评论

          本文标题:iframe与vue组件的交互

          本文链接:https://www.haomeiwen.com/subject/seeswktx.html