Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

vue组件中使用外部js方法

<div class="notic">{{ starPhone(phone) }}</div>
import { starPhone } from '@/utils/afterData'

utils/afterData

`export function starPhone(phoneNum) {
const start = phoneNum.slice(0, 3)
const end = phoneNum.slice(-4)
return ${start}****${end}
}`
报错
image.png
在methods中可以掉用starPhone()方法

如果在methods中写一个starPhone方法 页面中可以直接掉用,为什么引入的js方法在组件页面中不能直接使用呢

在methods中挂载一下方法
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如果要在template中使用变量或是函数,请先注册到data或是methods中:

<template>
    <div>{{title}} {{resolve(title)}}</div>
</template>

<script>
    export default {
        data() {
            return {
                title: '...'
            };
        },
        methods: {
            resolve(title) {
                return title;
            }
        }
    };
</script>

望采纳


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...