
- 先在点击 App.vue
- 在该页面的文件里的 export default 中写入我们要定义的全局变量
- 如App.vue代码如下
- <script>
- export default {
-
- //自己定义的代码
- //全局变量
- globalData:{
- api:"http://m.tnblog.net/api/v1",
- api_V2:"http://m.tnblog.net/api/v2",
- userinfo:{},
- token:"",
- },
-
-
- onLaunch: function()
- {
-
- console.log('App Launch')
-
- },
- onShow: function()
- {
-
- console.log('App Show')
-
- },
- onHide: function() {
-
- console.log('App Hide')
-
- }
-
- }
- </script>
- <style>
- /*每个页面公共css */
- </style>
-
- 3.引入过后使用,在显示界面 如我们创建一个空的uni-app 项目里的pages里的index界面代码如下
-
-
-
- <template>
- <view class="content">
- <image class="logo" src="/static/logo.png"></image>
- <view class="text-area">
- <text class="title">{{title}}</text>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- title: 'Hello'
- }
- },
- onLoad() {
-
- //1.先定义一个要执行的函数
- this.login();
- },
- methods: {
-
- //methods中执行
- login:function()
-
- {
- //只需要定义一个变量接收即可
-
- let Mydefined =getApp().globalData.定义的名字 如(api_V2)
-
- alert(getApp().globalData.api_V2)
- }
- }
- }
- </script>
- <style>
- .content {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- }
- .logo {
- height: 200rpx;
- width: 200rpx;
- margin-top: 200rpx;
- margin-left: auto;
- margin-right: auto;
- margin-bottom: 50rpx;
- }
- .text-area {
- display: flex;
- justify-content: center;
- }
- .title {
- font-size: 36rpx;
- color: #8f8f94;
- }
- </style>
评价