首页 文章

将javascript添加到vue组件<script>标记时,“ReferenceError:$未定义”

提问于
浏览
-1

尝试将代码添加到自动扩展文本框时,我收到此错误 - 从此codepen:https://codepen.io/vsync/pen/frudD . 我需要以不同的方式添加它吗?它目前位于vue组件文件中的脚本标记之间 .

Uncaught ReferenceError: $ is not defined
    at eval (Pastes.vue?58dd:23)
    at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/pages/pastes/Pastes.vue (app.js:782)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (Pastes.vue?fa7c:1)
    at Object../src/components/pages/pastes/Pastes.vue (app.js:1220)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (index.js?3672:1)
    at Object../src/router/index.js (app.js:1252)

2 回答

  • 1

    确保您正在加载jquery库,您可以关注此guide或此one .

  • 0

    我使用这个代码而不是很好

    https://jsfiddle.net/cferdinandi/mqwwpL6u/

    var autoExpand = function (field) {
    
        // Reset field height
        field.style.height = 'inherit';
    
        // Get the computed styles for the element
        var computed = window.getComputedStyle(field);
    
        // Calculate the height
        var height = parseInt(computed.getPropertyValue('border-top-width'), 10)
                     + parseInt(computed.getPropertyValue('padding-top'), 10)
                     + field.scrollHeight
                     + parseInt(computed.getPropertyValue('padding-bottom'), 10)
                     + parseInt(computed.getPropertyValue('border-bottom-width'), 10);
    
        field.style.height = height + 'px';
    
    };
    
    document.addEventListener('input', function (event) {
        if (event.target.tagName.toLowerCase() !== 'textarea') return;
        autoExpand(event.target);
    }, false);
    

相关问题